检查 JavaScript 对象中是否存在值

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

Check if value exists in JavaScript object

javascriptjquery

提问by ShaneKm

How would I check in my array of objects, if a specific item exists (in my case MachineId with id 2)?

如果存在特定项目(在我的情况下为 MachineId id 2),我将如何检查我的对象数组?

[{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}]

I tried this:

我试过这个:

if (index instanceof machineIds.MachineID) {
    alert('value is Array!');
} else {
    alert('Not an array');
}

回答by VisioN

In cross browser way you may use jQuery.grep()method for it:

在跨浏览器的方式你可以使用jQuery.grep()它的方法:

var item = $.grep(machineIds, function(item) {
    return item.MachineID == index;
});

if (item.length) {
    alert("value is Array!");
}

回答by hvgotcodes

The simplest to understand solution is to loop over the array, and check each one.

最容易理解的解决方案是遍历数组,并检查每个数组。

var match;
for (var i = 0; i < yourArray.length; i++) {
   if (yourArray[i].MachineId == 2) 
        match = yourArray[i];
}

Note if there is more than one matching item, this will return the last one. You can also dress this up in a function.

请注意,如果有多个匹配项,这将返回最后一项。你也可以用一个函数来修饰它。

function findByMachineId(ary, value) {
   var match;
    for (var i = 0; i < ary.length; i++) {
       if (ary[i].MachineId == value) 
            match = ary[i];
    }
    return match;
}

回答by freedev

There are many standardsolution, you don't need third party libraries or loop iteratively.

有很多标准解决方案,您不需要第三方库或迭代循环。

For example, using some();

例如,使用some();

var yourArray = [{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}];

var params = {searchedID: "2", elementFound: null};
var isCorrectMachineID = function(element) {
    if (element.MachineID == this.searchedID);
        return (this.elementFound = element);
    return false;
};

var isFound = yourArray.some(isCorrectMachineID, params)

Array somemethod accepts two parameters:

Arraysome方法接受两个参数:

  • callback - Function to test for each element.
  • thisObject - Object to use as this when executing callback.
  • callback - 测试每个元素的函数。
  • thisObject - 在执行回调时用作 this 的对象。

Callback function is not coupled with the iteration code and, using thisObject parameter, you can even return to the caller the element found or more data. If such an element is found, someimmediatelyreturns true

回调函数不与迭代代码耦合,使用 thisObject 参数,您甚至可以将找到的元素或更多数据返回给调用者。如果找到这样的元素,some立即返回真

http://jsfiddle.net/gu8Wq/1/

http://jsfiddle.net/gu8Wq/1/

回答by ericsoco

Old question at this point, but here's an ES6 solution that uses Array.find:

在这一点上的老问题,但这里有一个使用Array.find的 ES6 解决方案:

let machine2 = machines.find((machine) => machine.id === '2');
if (machine2) {
    // ...
}

回答by Asad Saeeduddin

You could use this condition:

你可以使用这个条件:

if (arr.filter(function(v){return this.MachineID == 2;}).length > 0)

回答by Kapilrc

var item = [{"MachineID":"1","SiteID":"20"},{"MachineID":"2","SiteID":"20"},{"MachineID":"3","SiteID":"20"},{"MachineID":"4","SiteID":"20"}];

var newItem = item.filter(function(i) {
  return i.MachineID == 2;  //it will return an object where MachineID matches with 2
});

console.log(newItem);  // will print [{"MachineID":"2","SiteID":"20"}]