How to remove multiple items from an array via looping in Javascript

jvc26

So, I have a javascript array of multiple values:

var keymap = {'name': 'foobaz', 
              'mappings': [{'id': 1, 'key': 'b'},
                          {'id': 2, 'key': 'c'},
                          {'id': 3, 'key': 'd'},
                          {'id': 1, 'key': 'e'},
                          {'id': 10, 'key': 'f'},
                          {'id': 7, 'key': 'g'},
                          {'id': 1, 'key': 'h'}]
}

I want to remove any entries where key is 'b'. Note the ids correspond to the backend ids. What I want to do is to remove some of the mappings (for example, all with 'id' as '1').

What I've tried is:

for (var i = 0; i < keymap['mappings'].length; i++) {
    if (keymap['mappings'][i]['id'] === id_to_match) {
        keyboard_map['mappings'].splice(i, 1);
    }
}

However, slice will change the indexes of the array in-place, and therefore now i won't point to the correct index point (as any indexes higher will now be i-n where n would be the number of slices done before.

What is the correct way to implement this?

Travis J

An easy way is to decrement the iterator (this works since you read the length every iteration - so make sure you avoid caching the length)

for (var i = 0; i < keymap['mappings'].length; i++) {
 if (keymap['mappings'][i]['id'] === id_to_match) {
    keyboard_map['mappings'].splice(i, 1);
    i--;
 }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How to remove multiple items from an array via looping in Javascript

From Dev

How to remove multiple sequential items of an array - JavaScript

From Dev

How to remove multiple items from a swift array?

From Dev

How to remove multiple items from a swift array?

From Dev

AngularFire how to remove multiple items from array?

From Dev

Remove items from array without looping

From Dev

How to remove multiple items in an array of objects in React?

From Dev

How to remove items from an array of objects?

From Dev

Remove items from array

From Dev

How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

From Dev

How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

From Dev

IBM Watson Assistant, how can I remove multiple items from an array?

From Dev

Remove Multiple Items from Array using underscore.js?

From Dev

Remove multiple items from array based on matched value

From Dev

Remove multiple object from array Javascript

From Dev

use JavaScript map reduce to remove items from an array

From Dev

function to remove single items or arrays from within an array - javascript

From Dev

How to remove array from array using Javascript?

From Dev

Remove multiple items from a slice

From Dev

Remove multiple items from ExpandableListView

From Dev

How to remove multiple values from an array at once

From Dev

Is it possible to remove items from a array?

From Dev

How to remove multiple items from a list, not just access them

From Dev

How could I remove the duplicated items(complex object) from array

From Dev

How do I safely remove items from an array in a for loop?

From Dev

How do I remove/filter specific items from an array?

From Dev

How to remove items with certain length from array in php

From Dev

How to deal with multiple items' states from FlastList( How to setState multiple items from the Array?)

From Dev

Javascript array with index and multiple items on it

Related Related

  1. 1

    How to remove multiple items from an array via looping in Javascript

  2. 2

    How to remove multiple sequential items of an array - JavaScript

  3. 3

    How to remove multiple items from a swift array?

  4. 4

    How to remove multiple items from a swift array?

  5. 5

    AngularFire how to remove multiple items from array?

  6. 6

    Remove items from array without looping

  7. 7

    How to remove multiple items in an array of objects in React?

  8. 8

    How to remove items from an array of objects?

  9. 9

    Remove items from array

  10. 10

    How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

  11. 11

    How do I compare two arrays and remove multiple items from one array that do not match in both arrays?

  12. 12

    IBM Watson Assistant, how can I remove multiple items from an array?

  13. 13

    Remove Multiple Items from Array using underscore.js?

  14. 14

    Remove multiple items from array based on matched value

  15. 15

    Remove multiple object from array Javascript

  16. 16

    use JavaScript map reduce to remove items from an array

  17. 17

    function to remove single items or arrays from within an array - javascript

  18. 18

    How to remove array from array using Javascript?

  19. 19

    Remove multiple items from a slice

  20. 20

    Remove multiple items from ExpandableListView

  21. 21

    How to remove multiple values from an array at once

  22. 22

    Is it possible to remove items from a array?

  23. 23

    How to remove multiple items from a list, not just access them

  24. 24

    How could I remove the duplicated items(complex object) from array

  25. 25

    How do I safely remove items from an array in a for loop?

  26. 26

    How do I remove/filter specific items from an array?

  27. 27

    How to remove items with certain length from array in php

  28. 28

    How to deal with multiple items' states from FlastList( How to setState multiple items from the Array?)

  29. 29

    Javascript array with index and multiple items on it

HotTag

Archive