简体   繁体   中英

How to select a Array index element with respect of its one of Value?

I have a Array

    var arr=[{t: "Lokanath", v: 0},
            {t: "Das", v: 1}]

Is there any way that i can get the Record t: "Das", v: 1 based on the value of v ie v:1

Array.prototype.filter()

The filter() method creates a new array with all elements that pass the test implemented by the provided function.

You can use filter() in the following way:

 var a=[{text: "Lokanath", value: 0, id: 100}, {text: "Das", value: 1, id: 101}]; var das = a.filter(p => p.text=='Das'); console.log(das[0]);

Please Note: Since filter() returns an array, you have to use index to take the object.

var arr=[{text: "Lokanath", value: 0, id: 100},
            {text: "Das", value: 1, id: 101}]

arr.find(d => d.text == "Das")

you can use filter method to do that.

var arr=[{text: "Lokanath", value: 0, id: 100},{text: "Das", value: 1, id: 101}];
console.log(arr.filter(o => o.text=='Das')[0]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM