0

How can I remove the particular item from the array loop if the condition matches?

array.map(item => {
   item.id === companyId ? 
      //how to remove this item from the array??
} : null)

Thanks in advance

1
  • Run filter on the array first: array.filter( item => item.id !== companyId ).map( ... ) Commented May 23, 2019 at 8:27

2 Answers 2

2

You can use the native JS .filter method

const newArray = array.filter(item => item.id !== companyId)

This will return a new array without the matching item.

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

Comments

2

You can use the the filter function:

var data = [{id: 1}, {id: 2}, {id: 3}];

const result = data.filter(val => val.id != 2);

console.log(result);

1 Comment

You can also use _.remove from lodash const newArray = _.remove(array, item => item.id === companyId)

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.