通过javascript对象迭代查找匹配项[重复]

时间:2022-09-14 16:51:42

This question already has an answer here:

这个问题在这里已有答案:

I'm trying to loop through an object and find a specific match. When I find a match, I want to pull the other key value pairs in the object. So if I have an object in this form:

我正在尝试遍历一个对象并找到一个特定的匹配。当我找到匹配项时,我想拉出对象中的其他键值对。所以,如果我有一个这种形式的对象:

[{"ID":"3","NAME":"John","Age":"15"},{"ID":"4","NAME":"Mike","Age":"10"},{"ID":"5","NAME":"Tim","Age":"18"},{"ID":"6","NAME":"Ken","Age":"20"}]

and I have a search/match value

我有一个搜索/匹配值

var MatchValue = 4;

How can I iterate through my object to find the "ID" that equals 4 and display the "NAME" and "AGE" from the object? Any help on this would be greatly appreciated. Thanks in advance. #JsonNoob.

如何遍历我的对象以找到等于4的“ID”并显示对象的“NAME”和“AGE”?任何有关这方面的帮助将不胜感激。提前致谢。 #JsonNoob。

1 个解决方案

#1


0  

What you are looking to do is filter your array. You can do this with JavaScript's filter function. This code will get you the elements where the ID is equal to 4.

您要做的是过滤您的阵列。您可以使用JavaScript的过滤功能执行此操作。此代码将为您提供ID等于4的元素。

You can then access this object's properties with either dot or bracket notation.

然后,您可以使用点或括号表示法访问此对象的属性。

var myObject = array.filter(function(element) {
    return element.ID === '4';
}

console.log(myObject.name);     // Prints name to console
console.log(myObject.age);      // Prints age to console

If you have a browser from the stone ages, you can do this

如果你有一个石器时代的浏览器,你可以这样做

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function")
    throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this) {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
        res.push(val);
      }
    }
    return res;
  };
}

I got this from another Stack Overflow answer, but I can explain it.

我从另一个Stack Overflow回答得到了这个,但我可以解释一下。

This checks if your browser has the Array.prototype.filter function. Some older versions of IE don't have it, so you can then define the function manually.

这将检查您的浏览器是否具有Array.prototype.filter函数。某些旧版本的IE没有它,因此您可以手动定义该功能。

#1


0  

What you are looking to do is filter your array. You can do this with JavaScript's filter function. This code will get you the elements where the ID is equal to 4.

您要做的是过滤您的阵列。您可以使用JavaScript的过滤功能执行此操作。此代码将为您提供ID等于4的元素。

You can then access this object's properties with either dot or bracket notation.

然后,您可以使用点或括号表示法访问此对象的属性。

var myObject = array.filter(function(element) {
    return element.ID === '4';
}

console.log(myObject.name);     // Prints name to console
console.log(myObject.age);      // Prints age to console

If you have a browser from the stone ages, you can do this

如果你有一个石器时代的浏览器,你可以这样做

if (!Array.prototype.filter) {
  Array.prototype.filter = function(fun /*, thisp*/) {
    var len = this.length >>> 0;
    if (typeof fun != "function")
    throw new TypeError();

    var res = [];
    var thisp = arguments[1];
    for (var i = 0; i < len; i++) {
      if (i in this) {
        var val = this[i]; // in case fun mutates this
        if (fun.call(thisp, val, i, this))
        res.push(val);
      }
    }
    return res;
  };
}

I got this from another Stack Overflow answer, but I can explain it.

我从另一个Stack Overflow回答得到了这个,但我可以解释一下。

This checks if your browser has the Array.prototype.filter function. Some older versions of IE don't have it, so you can then define the function manually.

这将检查您的浏览器是否具有Array.prototype.filter函数。某些旧版本的IE没有它,因此您可以手动定义该功能。