检查JavaScript对象中是否存在值

时间:2022-03-05 23:45:05

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

如果存在一个特定的项(在我的情况下,使用id 2的MachineId),我如何检查我的对象数组?

[{"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');
}

6 个解决方案

#1


12  

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!");
}

#2


5  

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

#3


4  

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

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

For example, using 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 some method accepts two parameters:

数组some方法接受两个参数:

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

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, some immediately returns true

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

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

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

#4


2  

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

这是一个老问题,但这里有一个使用array的ES6解决方案。

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

#5


1  

You could use this condition:

您可以使用以下条件:

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

#6


0  

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

#1


12  

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!");
}

#2


5  

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

#3


4  

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

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

For example, using 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 some method accepts two parameters:

数组some方法接受两个参数:

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

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, some immediately returns true

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

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

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

#4


2  

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

这是一个老问题,但这里有一个使用array的ES6解决方案。

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

#5


1  

You could use this condition:

您可以使用以下条件:

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

#6


0  

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