0

Imagine something like:

const newItem = ['item 1', 'item 0', 'item 1', 'item 2', 'item 1', 'item 0'];

If I want to remove all 'item 1' I can use:

for (let i = newItem.length-1; i--;){
    if (newItem[i] === "item 1") newItem.splice(i, 1);
}

The question is if I have an array inside another array how can I do?

const newItem = [
    ['item 1', 'item 0', 'item 1'],
    ['item 2', 'item 1', 'item 0']
];

Thanks!

4
  • 1
    arr = arr.map(subArr => subArr.filter(element => element !== 'item 1')) Commented Apr 18, 2019 at 12:24
  • @Seblor it is always good to use one liner but OP seems to be new in this so I would suggest a detailed way using for or forEach() Commented Apr 18, 2019 at 12:25
  • Sure, but this is why I wrote it as a comment not an answer. Writing the one liner here can be a way to introduce OP to the functional programming side of JS. Commented Apr 18, 2019 at 12:27
  • do you want to keep the object reference or is a new array ok? what about empty array after filtering/splicing? Commented Apr 18, 2019 at 12:27

4 Answers 4

3

You can use map and filter.map will return a new array and inside the callback check if the item which is ['item 1', 'item 0', 'item 1'] & ['item 2', 'item 1', 'item 0'] includes item 1

const newItem = [
  ['item 1', 'item 0', 'item 1'],
  ['item 2', 'item 1', 'item 0']
];

let k = newItem.map(function(item) {
  return item.filter(elm => elm !== 'item 1')
});

console.log(k)
//newItem is not changed
console.log(newItem)

Sign up to request clarification or add additional context in comments.

7 Comments

Is there a reason for item.includes ? (Aside from reducing the number of loops)
INCORRECT, The value of newItem is not changed. You actually need to remove value from that array
@AnkitAgarwal it is changed, since map is used. You can even run the snippet to see it work.
@Seblor check the snippet it dont work. newItem is not changed. We expect that array to get changed with no item1
@AnkitAgarwal Ah right. You just need to affect the return value from map to newItem.
|
2

You can use a combination of map and filter:

const newItem = [
	['item 1', 'item 0', 'item 1'],
	['item 2', 'item 1', 'item 0']
];
console.log(newItem.map(a => a.filter(e => e !== 'item 1')));

Comments

1

Just use nested forEach() loop:

const newItem = [
  ['item 1', 'item 0', 'item 1'],
  ['item 2', 'item 1', 'item 0']
];
newItem.forEach((innerArray) => {
  innerArray.forEach((innerItem, i) => {
    if (innerItem === "item 1") innerArray.splice(i, 1);
  });
});
console.log(newItem);

1 Comment

Thanks everyone for answering and helping with my question!
1

If you know which index you want to access, one way to easily access this:

var arr = [[1,2,3], [5,6,7]];
console.log(arr[0][1]); // This will print 2, the second element of the first array item.

You can also easily iterate the nested array items using nested loop:

var arr = [
  ["item1", "item2", "item3"], ["item1", "item2", "item3"]];
arr.forEach(function(arrayItem){
    arrayItem.forEach(function(item, index, array){
         if(item === "item1"){
             array.splice(index, 1); // Removes the matching item from the arrayItem.
         }
    });
});

1 Comment

If you downvote, kindly provide a reason why so that I can improve my answer or for future reference in case I missed something.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.