0

I'm looking for a way, how remove array items with compare of other array.

keys = ['201','300'];

obj[0]=['someval','somename','201'];
obj[1]=['someval','somename','241'];
obj[2]=['someval','somename','300'];
obj[3]=['someval','somename','230'];

Now, how can i have obj array with elements equal to keys array ? I want to archive this:

obj[0]=['someval','somename','201'];
obj[1]=['someval','somename','300'];

Thank You for help.

1 Answer 1

1

You can use .filter() and check whether the 3rd item in the inner array is present in the keys array

keys = ['201', '300'];

var obj = []
obj[0] = ['someval', 'somename', '201'];
obj[1] = ['someval', 'somename', '241'];
obj[2] = ['someval', 'somename', '300'];
obj[3] = ['someval', 'somename', '230'];

obj = obj.filter(function(item) {
  return keys.indexOf(item[2]) > -1
})

console.log(obj)

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

4 Comments

@user2610146 what do you mean by custom array.... which part of the array is custom
obj array may have more values, example: obj[3] = ['someval', 'somename','testval' '230','other'];
do you know which index in the array to be checked
And what will happens, when obj will be array like obejct instead of array ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.