首页信息安全makeanarrayofitemfromanotherarrayinjquery

makeanarrayofitemfromanotherarrayinjquery

admin 04-09 15:42 152次浏览

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);
makeanarrayofitemfromanotherarrayinjquery
内容可编辑正则表达式无法按预期工作 ubuntu上怎么安装golang
相关内容