0

I am new to JS. I have multidimensional array.

var myList = [{"Fund":"Edelweiss1","Rank":10},{"Fund":"Edelweiss","Rank":5}, {"Fund":"Edelweiss3","Rank":3}]

I want to filter "Rank" based on another array.

var rngarray = [6,10];

Filtering on rank should be >=6 and <=10

I tried this - myList.filter(function (v) {rngarray.includes(v["Rank"])}) but does not work.

1 Answer 1

1

Use Array.prototype.filter():

var myList = [{"Fund":"Edelweiss1","Rank":10},{"Fund":"Edelweiss","Rank":5}, {"Fund":"Edelweiss3","Rank":3}];

var rngarray = [6,10];

var filtered = myList.filter(item => item.Rank >= rngarray[0] && item.Rank <= rngarray[1]);
console.log(filtered);

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

2 Comments

Thanks. Works like a charm. Can you please explain why this does not work - myList.filter(function (v) {v.Rank >= rngarray[0] && v.Rank <= rngarray[1]})
myList.filter(function (v) {v.Rank > when you use function you need a return: myList.filter(function (v) { return v.Rank > - when using item => without { the return is implied.

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.