0

Given the array of objects:

var items = [
{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"0","color":"green"},
{"rank":"6","color":"blue"},
{"rank":"0","color":"yellow"}
];

I want to remove all the enries with rank 0, so that the result will be:

Items:

[{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"6","color":"blue"}];

6 Answers 6

1

You can just add a filter to the Items array :

items.filter(item => item.rank > 0)
Sign up to request clarification or add additional context in comments.

Comments

1

Apply filter :

var items = [{"rank":"3","color":"red"},{"rank":"4","color":"blue"},{"rank":"0","color":"green"},{"rank":"6","color":"blue"},{"rank":"0","color":"yellow"}];

result = items.filter(({rank})=>rank>0);
console.log(result);

Comments

1

You can use Array.filter method to do that.

items = items.filter(i=> {
   return i.rank !== '0';
});

Comments

1

var items = [
{"rank":"3","color":"red"},
{"rank":"4","color":"blue"},
{"rank":"0","color":"green"},
{"rank":"6","color":"blue"},
{"rank":"0","color":"yellow"}
];

let filteredArray = items.filter(el => el.rank > 0);

console.log(filteredArray);

Comments

1

Try to filter by trusy value and apply + sign to convert from string to number:

var items = [
   {"rank":"3","color":"red"},
   {"rank":"4","color":"blue"},
   {"rank":"0","color":"green"},
   {"rank":"6","color":"blue"},
   {"rank":"0","color":"yellow"}
];

result = items.filter(({rank})=> +rank);
console.log(result);

1 Comment

1

you can apply a filter like so:

items = items.filter(el => el.rank != "0")

Comments

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.