admin管理员组文章数量:1316376
I am new to Typescript. I have an Array of type in typescript.Basically containing the collection elements as
"ID": "669a8156-528c-43ba-8ed0-d07874534d1c",
"Name": "Temple",
"DeviceCount": "0",
"SiteCount": "0"
"ID": "5965ee85-2300-4c95-8743-b626f744082f",
"Name": "Building",
"DeviceCount": "2",
"SiteCount": "3"
..so on
How do I query the Name property from the collection if I have the ID
i.e., something similar to LINQ type of expression
var result = array.Where(item => item.ID == ID);
I am new to Typescript. I have an Array of type in typescript.Basically containing the collection elements as
"ID": "669a8156-528c-43ba-8ed0-d07874534d1c",
"Name": "Temple",
"DeviceCount": "0",
"SiteCount": "0"
"ID": "5965ee85-2300-4c95-8743-b626f744082f",
"Name": "Building",
"DeviceCount": "2",
"SiteCount": "3"
..so on
How do I query the Name property from the collection if I have the ID
i.e., something similar to LINQ type of expression
var result = array.Where(item => item.ID == ID);
Share
Improve this question
edited Jun 23, 2016 at 4:21
Ryan Cavanaugh
221k59 gold badges278 silver badges235 bronze badges
asked Jun 23, 2016 at 4:13
this-Methis-Me
2,1457 gold badges44 silver badges71 bronze badges
0
1 Answer
Reset to default 7You can use the JavaScript Array#filter
method for this, which returns an array of matches, very similar to your LINQ code:
array.filter(item => item.ID === ID)[0].name;
You could also use Array#find
, but that doesn't have very good browser support, so you might need a polyfill for Opera and Internet Explorer:
array.find(item => item.ID === ID).name;
Read documentation about it: https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter
本文标签: javascriptQuerying Typescript array collection based on keyStack Overflow
版权声明:本文标题:javascript - Querying Typescript array collection based on key - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742002656a2411328.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论