makeanarrayofitemfromanotherarrayinjquery
I have following array
var Fruits = [ { "Name": "Apple,Orange", "id": "20" }, { "Name": "Mango", "id": "40" } ];
I want to make a new array that contains Name of selected item whose id matches the given id. Something like we do in C# linq:
var givenId = 20 var newArray = fruits.Select(x=>x.Name).Where(y=>y.id == givenId)
so my newArray becomes ['Apple', 'Orange']
1> Cuong Le Ngo..:
您可以使用Array.prototype.filter() 和Array.prototype.map() 来做到这一点。
演示:
var Fruits = [
{
"Name": "Apple,Orange",
"id": "20"
},
{
"Name": "Mango",
"id": "40"
}
];
var given_id = 20;
var result = Fruits.filter(val => val.id == given_id).map(val => val.Name);
console.log(result);