Javascript 在Javascript中的对象数组中查找值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12462318/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 08:01:56  来源:igfitidea点击:

Find a value in an array of objects in Javascript

javascriptarrays

提问by Arlen Beiler

I know similar questions have been asked before, but this one is a little different. I have an array of unnamed objects, which contain an array of named objects, and I need to get the object where "name" is "string 1". Here is an example array.

我知道以前有人问过类似的问题,但这个问题有点不同。我有一个未命名对象数组,其中包含一个命名对象数组,我需要获取“名称”为“字符串 1”的对象。这是一个示例数组。

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

Update:I should have said this earlier, but once I find it, I want to replace it with an edited object.

更新:我应该早点说这个,但是一旦我找到它,我想用一个编辑过的对象替换它。

采纳答案by Asciiom

You can loop over the array and test for that property:

您可以遍历数组并测试该属性:

function search(nameKey, myArray){
    for (var i=0; i < myArray.length; i++) {
        if (myArray[i].name === nameKey) {
            return myArray[i];
        }
    }
}

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

var resultObject = search("string 1", array);

回答by ?ime Vidas

Finding the array element:

查找数组元素:

let arr = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

let obj = arr.find(o => o.name === 'string 1');

console.log(obj);



Replacing the array element:

替换数组元素:

let arr = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

let obj = arr.find((o, i) => {
    if (o.name === 'string 1') {
        arr[i] = { name: 'new string', value: 'this', other: 'that' };
        return true; // stop searching
    }
});

console.log(arr);

回答by rujmah

In ES6you can use Array.prototype.find(predicate, thisArg?)like so:

ES6 中,您可以Array.prototype.find(predicate, thisArg?)像这样使用:

array.find(x => x.name === 'string 1')

http://exploringjs.com/es6/ch_arrays.html#_searching-for-array-elementshttps://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

http://exploringjs.com/es6/ch_arrays.html#_searching-for-array-elements https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find

To then replace said object (and use another cool ES6method fill) you could do something like:

然后替换所述对象(并使用另一个很酷的ES6方法fill),您可以执行以下操作:

let obj = array.find(x => x.name === 'string 1');
let index = array.indexOf(obj);
array.fill(obj.name='some new string', index, index++);

回答by p.pradnya

As per ECMAScript 6, you can use the findIndexfunction.

根据 ECMAScript 6,您可以使用该findIndex函数。

array[array.findIndex(x => x.name == 'string 1')]

回答by Zahirul Haque

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

var foundValue = array.filter(obj=>obj.name==='string 1');

console.log(foundValue);

回答by Gaurav Sharma

Considering you have following snippet:

考虑到您有以下代码段:

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

You can use the following function to search for items

您可以使用以下功能来搜索项目

const search = what => array.find(element => element.name === what);

And you can check whether the item was found or not.

您可以检查是否找到了该项目。

if (search("string 1")) {
    console.log(search.value, search.other);
} else {
    console.log('No result found');
}

回答by Wallace Maxters

with underscore.jsuse the findWhere method:

underscore.js使用findWhere方法:

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];


var result = _.findWhere(array, {name: 'string 1'});

console.log(result.name);

See this in JSFIDDLE

JSFIDDLE 中看到这个

回答by Jo?o Silva

Either use a simple for-loop:

要么使用简单的for-loop:

var result = null;
for (var i = 0; i < array.length; i++) { 
  if (array[i].name === "string 1") { 
    result = array[i];
    break;
  } 
}

Or if you can, that is, if your browser supports it, use Array.filter, which is much more terse:

或者如果可以,也就是说,如果您的浏览器支持它,请使用Array.filter,它更简洁:

var result = array.filter(function (obj) {
  return obj.name === "string 1";
})[0];

回答by Durgpal Singh

One line answer. You can use filter function to get result.

一行回答。您可以使用过滤器功能来获得结果。

var array = [
    { name:"string 1", value:"this", other: "that" },
    { name:"string 2", value:"this", other: "that" }
];

console.log(array.filter(function(arr){return arr.name == 'string 1'})[0]);

回答by MarvinVK

With a foreach:

使用 foreach:

let itemYouWant = null;
array.forEach((item) => {
    if (item.name === 'string 1') {
        itemYouWant = item;
    }
});
console.log(itemYouWant);