-1

I have a dynamic array (chat messages), like

{
 id:1,
 message: bla-bla
},
{
 id:2,
 message: bla-bla
},
{
 id:1,
 message: bla-bla
},
{
 id:1,
 message: bla-bla
},
{
 id:3,
 message: bla-bla
},
{
 id:4,
 message: bla-bla
}

How to leave in array only items with id:1, or remove all items where id not 1

0

1 Answer 1

4

You can make use of .filter() and only return items where the id is equal to 1.

var newArr = yourArray.filter(function(item) {
  return item.id === 1
});

jsFiddle

And for more info and the .filter() shim for older browsers - click here

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

3 Comments

At the end point I need to have an array with items where id == 1
@Serhiog.Lazin My bad, i've made the amendment
Thanks very much! Simple and amazing!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.