TypeScript——根据属性值从数组中取出对象

时间:2022-07-27 22:30:50

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

取物体在哪里。var id =

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个条目,所以我想知道是否有更有效的方法

4 个解决方案

#1


20  

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

数组中。在MDN找到:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

#2


1  

I'd use filter or reduce:

我会使用过滤器或减少:

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"}

(code in playground)

在操场上(代码)

If you care about iterating over the entire array then use some:

如果您关心遍历整个数组,那么使用以下方法:

let item;
array.some(i => {
    if (i.id === 1) {
        item = i;
        return true;
    }
    return false;
});

(code in playground)

在操场上(代码)

#3


1  

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:

你需要对数组进行循环,但是如果你创建一个hashmap来链接每个id到一个索引并保存它,你只需要做一次,所以你可以直接引用后面的任何objeft:

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都是唯一的。

#4


0  

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中创建搜索过滤管道

#1


20  

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

数组中。在MDN找到:https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

#2


1  

I'd use filter or reduce:

我会使用过滤器或减少:

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"}

(code in playground)

在操场上(代码)

If you care about iterating over the entire array then use some:

如果您关心遍历整个数组,那么使用以下方法:

let item;
array.some(i => {
    if (i.id === 1) {
        item = i;
        return true;
    }
    return false;
});

(code in playground)

在操场上(代码)

#3


1  

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:

你需要对数组进行循环,但是如果你创建一个hashmap来链接每个id到一个索引并保存它,你只需要做一次,所以你可以直接引用后面的任何objeft:

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都是唯一的。

#4


0  

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中创建搜索过滤管道