Javascript TypeScript - 根据属性值从数组中取出对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42580100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
TypeScript - Take object out of array based on attribute value
提问by Nicolas
My array looks like this:
我的数组如下所示:
array = [object {id: 1, value: "itemname"}, object {id: 2, value: "itemname"}, ...]
all my objects have the same attibutes, but with different values.
我所有的对象都具有相同的属性,但具有不同的值。
Is there an easy way I can use a WHERE statement for that array?
有没有一种简单的方法可以为该数组使用 WHERE 语句?
Take the object where object.id = var
取对象 where object.id = var
or do I just need to loop over the entire array and check every item? My array has over a 100 entries, so I wanted to know if there was a more efficient way
还是我只需要遍历整个数组并检查每个项目?我的数组有 100 多个条目,所以我想知道是否有更有效的方法
回答by Saravana
Use Array.find:
使用Array.find:
let array = [
{ id: 1, value: "itemname" },
{ id: 2, value: "itemname" }
];
let item1 = array.find(i => i.id === 1);
Array.find at MDN: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find
Array.find 在 MDN:https: //developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find
回答by Nitzan Tomer
let array = [
{ id: 1, value: "itemname" },
{ id: 2, value: "itemname" }
];
let item1 = array.filter(item => item.id === 1)[0];
let item2 = array.reduce((prev, current) => prev || current.id === 1 ? current : null);
console.log(item1); // Object {id: 1, value: "itemname"}
console.log(item2); // Object {id: 1, value: "itemname"}
(操场上的代码)
If you care about iterating over the entire array then use some:
如果您关心遍历整个数组,请使用some:
let item;
array.some(i => {
if (i.id === 1) {
item = i;
return true;
}
return false;
});
(操场上的代码)
回答by Aamir
You can search a certain value in array of objects using TypeScript dynamically if you need to search the value from all fields of the object without specifying column
如果您需要在不指定列的情况下从对象的所有字段中搜索值,您可以使用 TypeScript 动态搜索对象数组中的某个值
var searchText = 'first';
let items = [
{ id: 1, name: "first", grade: "A" },
{ id: 2, name: "second", grade: "B" }
];
This below code will search for the value
var result = items.filter(item =>
Object.keys(item).some(k => item[k] != null &&
item[k].toString().toLowerCase()
.includes(searchText.toLowerCase()))
);
Same approach can be used to make a Search Filter Pipe in angularjs 4 using TypeScript
可以使用相同的方法使用 TypeScript 在 angularjs 4 中制作搜索过滤器管道
回答by Shilly
You'll have to loop over the array, but if you make a hashmap to link each id to an index and save that, you only have to do it once, so you can reference any objeft after that directly:
您必须遍历数组,但是如果您制作一个哈希图将每个 id 链接到一个索引并保存它,您只需执行一次,这样您就可以直接引用此后的任何对象:
var idReference = myArray.reduce(function( map, record, index ) {
map[ record.id ] = index;
return map;
}, {});
var objectWithId5 = myArray[ idReference["5"] ];
This does assume all ids are unique though.
这确实假设所有 id 都是唯一的。

