JavaScript Programmatically modifying maps based on inner condition

You could iterate the map and if the wanted value is found, filter the array and assign the filtered array.

const map = new Map([['123', [['foo', 'bar']]], ['456', [['baz', 'qux'], ['quux', 'corge']]]]);

map.forEach((v, k, m) => {
    if (v.some(a => a[0] === 'quux')) {
        m.set(k, v.filter(a => a[0] !== 'quux'));
    }
});

console.log([...map]);
Answer:1

You can loop over the values of the Map, use findIndex on each value v to see if it includes an array whose first element is quux, and splice that array out if so:

const map = new Map().set('123', [ [ 'foo', 'bar' ] ]).set('456', [ [ 'baz', 'qux' ], [ 'quux', 'corge' ] ]);

console.log("before", [...map]);

for (const v of map.values()) {
  const index = v.findIndex((a) => a[0] === "quux");
  
  if (index > -1) {
    v.splice(index, 1);
  }
}

console.log("after", [...map]);
Answer:2

You could do the key dynamically with a for of loop like this:

BTW open your devtools to checkout the new map since map cannot be properly displayed in the code snippet.

const Map = new Map().set('123', [
  ['foo', 'bar']
]).set('456', [
  ['baz', 'qux'],
  ['quux', 'corge']
]);


for (let el of Map) {
  Map.set(el[0], (Map.get(el[0])).filter(array => array[0] !== 'quux'));
}


console.log(Map);
Answer:3

Iterate over key-value pair of the map, the value will have the outer array from which we can filter out the inner array having the value we are looking for. We can get the index of the inner array from the forEach function, using which we can use the splice function to remove the inner array from the outer array.

const map = new Map().set('123', [ [ 'foo', 'bar' ] ]).set('456', [ [ 'baz', 'qux' ], [ 'quux', 'corge' ] ]);
map.forEach((v, k)=>
{
     v.forEach((arr, idx)=> {
     if(arr.includes('quux')){
            v.splice(idx,1);
        }
    },)
});
console.log(map);
Answer:4

I have a navbar which needs to change colour on scroll. This functionality was originally built with js, however now it has a 'Login' button which is built in elm to perform other functionality. I ...

I have a navbar which needs to change colour on scroll. This functionality was originally built with js, however now it has a 'Login' button which is built in elm to perform other functionality. I ...

Prelog: I have implemented Google maps and Geolocation as a independent widgets, Now the user have the ability to add the widget as much as he wants in a page he is owns. I am using $.getScript(URl, ...

Prelog: I have implemented Google maps and Geolocation as a independent widgets, Now the user have the ability to add the widget as much as he wants in a page he is owns. I am using $.getScript(URl, ...

  1. you have included the google maps

I have Table (Employees) A: +----+---------+ | ID | NAME | +----+---------+ | 1 | ROBERT | | 2 | JAMES | | 3 | RICHARD | | 4 | KANYE | | 5 | DYLAN | | 6 | JOHN | | 7 | JEAN | | ...

I have Table (Employees) A: +----+---------+ | ID | NAME | +----+---------+ | 1 | ROBERT | | 2 | JAMES | | 3 | RICHARD | | 4 | KANYE | | 5 | DYLAN | | 6 | JOHN | | 7 | JEAN | | ...

  1. hide rows table jquery
  2. hide rows table html
  3. hide rows table
  4. hide rows table in word
  5. hide table rows javascript
  6. hide table rows css
  7. hide table rows in confluence
  8. hide table rows in word 2016
  9. hide table rows excel
  10. hide table rows using css
  11. hide table rows using jquery
  12. hide table rows using javascript
  13. hide table rows with empty td element
  14. hide table rows asp.net
  15. hide table rows if empty
  16. hide table rows filter
  17. hide table rows with button
  18. hidden table rows css
  19. hide rows pivot table
  20. hide rows in table access

I have the following path: /s/STRINGINEED/abcdef/ How should I structure my regex to match STRINGINEED as a result? /s/ is a fixed path, so I would like to get any string between /s/ and the ...

I have the following path: /s/STRINGINEED/abcdef/ How should I structure my regex to match STRINGINEED as a result? /s/ is a fixed path, so I would like to get any string between /s/ and the ...

  1. find specific string in excel
  2. find specific string in python
  3. find specific string in list python
  4. find specific string in php
  5. find specific string regex
  6. find specific string in linux
  7. find specific string in excel cell
  8. find specific string matlab
  9. find specific string in file python
  10. find specific string in c#
  11. find specific string in file linux
  12. find specific string in array javascript
  13. find specific string in sql
  14. find specific string in text file powershell
  15. find specific string in all files linux
  16. find specific string in javascript
  17. find specific string in stored procedure
  18. find specific string in sql database
  19. find specific string in r
  20. find specific string in jquery