JavaScript Remove array from inside array based on index in JS remove array from array,remove array from array python,remove

I have an array which looks like:-

[[0,1], [0,2], [0,3], [1,1], [1,2]...]

I am looking to remove one of the arrays from this array based on the indexOf() but I keep getting a value of -1, which removes the last item from the array when I try the following code:-

array = [[0,1], [0,2], [0,3], [1,1], [1,2]];
console.log('Removed value', array.splice(array.indexOf([0,3]), 1));
console.log('Result', array);
Answer:1

You can't use indexOf because when you declare [0,3] in array.splice(array.indexOf([0,3]), 1)) you're creating a new array and this new object is not inside your array (but rather another array that has the same values).

You can use findIndex instead as follows (example):

array.findIndex(x => x[0] === 0 && x[1] === 3)

this will return 2 - now you can use it to delete:

array.splice(2, 1)
Answer:2

If it is OK to remove every occurrence of [0,3], then consider Array.filter combined with array destructuring of the lambda arguments. It offers a slightly leaner syntax than the other solutions.

const input = [
    [0,1],
    [0,2],
    [0,3],
    [1,1],
    [1,2]
];


const result = input.filter(([x,y]) => !(x==0 && y==3));
console.log('Result=', result);
Answer:3

I am trying to import several thousand records into a nested array in a collection in Meteor. This is financial data coming in a JSON object. I need to do some calculations on it before insertings it, ...

I am trying to import several thousand records into a nested array in a collection in Meteor. This is financial data coming in a JSON object. I need to do some calculations on it before insertings it, ...

  1. meteor bulk insert
  2. meteor mongodb bulk insert

I am working on one input application where i need to test input values that accepts single, multiple and even a range of numbers . Eg inputs : 70,900,80-20 // should return true as all are valid as,...

I am working on one input application where i need to test input values that accepts single, multiple and even a range of numbers . Eg inputs : 70,900,80-20 // should return true as all are valid as,...

  1. angular test input values

i created a crypto object as follows: var crypto = { encrypt: function(s) { } }; crypto.encrypt("cat"); I would get the following error Uncaught TypeError: crypto.encrypt is ...

i created a crypto object as follows: var crypto = { encrypt: function(s) { } }; crypto.encrypt("cat"); I would get the following error Uncaught TypeError: crypto.encrypt is ...

I try to run my test.js file below with the command: DEBUG=nightmare node --harmony test.js and taking output: nightmare queueing action "goto" for http://google.com +0ms nightmare queueing ...

I try to run my test.js file below with the command: DEBUG=nightmare node --harmony test.js and taking output: nightmare queueing action "goto" for http://google.com +0ms nightmare queueing ...