基于键查询Typescript数组集合 [英] Querying Typescript array collection based on key

查看:314
本文介绍了基于键查询Typescript数组集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Typescript的新手.我在typescript中有一个类型为Array的元素,基本上包含

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


..so on

如果我具有 ID

即类似于LINQ类型的表达式

i.e., something similar to LINQ type of expression

var result = array.Where(item => item.ID == ID);

推荐答案

您可以为此使用JavaScript Array#filter方法,该方法返回匹配数组,非常类似于LINQ代码:

You 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;

您也可以使用Array#find,但是它没有很好的浏览器支持,因此您可能需要Opera和Internet Explorer的polyfill:

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;

阅读有关此文档的文档: https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

Read documentation about it: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/filter

这篇关于基于键查询Typescript数组集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆