jQuery 在 JSON 数组中搜索字符串并检索包含它作为值的对象

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

Search JSON Array for a String and Retrieve Object that Contains it as a Value

javascriptjqueryjson

提问by ac360

My JSON is below. It contains two Objects, each with a few key value pairs. How can I search through the entire JSON array and pull the object that contains a particular string as a value?

我的 JSON 在下面。它包含两个对象,每个对象都有几个键值对。如何搜索整个 JSON 数组并将包含特定字符串的对象作为值提取?

In this case, I need to pull the object with the coupon_code: COUPON1, so that I can then pull the ID of that Coupon.

在这种情况下,我需要使用coupon_code:COUPON1拉取对象,以便我可以拉取该Coupon的ID。

In short, I just need to get the id of the Coupon with coupon_code: COUPON1

简而言之,我只需要使用coupon_code获取Coupon的id:COUPON1

[Object, Object]

  0: Object
  coupon_code: "COUPON1"
  created_at: "2013-06-04T13:50:20Z"
  deal_program_id: 1
  id: 7
  updated_at: "2013-06-04T13:50:20Z"
  __proto__: Object

  1: Object
  coupon_code: "COUPON3"
  created_at: "2013-06-04T15:47:14Z"
  deal_program_id: 1
  id: 8
  updated_at: "2013-06-04T15:47:14Z"

Thanks :)

谢谢 :)

回答by T.J. Crowder

You just loop through the array and look. There are lots of ways to do that in JavaScript.

您只需遍历数组并查看即可。在 JavaScript 中很多方法可以做到这一点

E.g.:

例如:

var a = /*...your array...*/;
var index = 0;
var found;
var entry;
for (index = 0; index < a.length; ++index) {
    entry = a[index];
    if (entry.coupon_code == "COUPON1") {
        found = entry;
        break;
    }
}

Or using ES5's Array#somemethod (which is one that can be "shimmed" for browsers that don't yet have it, search for "es5 shim"):

或者使用 ES5 的Array#some方法(对于还没有它的浏览器,可以“填充”一种方法,搜索“es5 shim”):

var a = /*...your array...*/;
var found;
a.some(function(entry) {
    if (entry.coupon_code == "COUPON1") {
        found = entry;
        return true;
    }
});

回答by HBP

Write a generic find function :

编写一个通用的查找函数:

function find (arr, key, val) { // Find array element which has a key value of val 
  for (var ai, i = arr.length; i--;)
    if ((ai = arr[i]) && ai[key] == val)
      return ai;
  return null;
}

Call as follows :

调用如下:

find (arr, 'coupon_code', 'COUPON1')

回答by RienNeVaPlu?s

var result = null;
Objects.forEach(function(obj, i){
    if(obj.cupon_code == 'COUPON1'){
        return result = obj;
    }
});
console.log(result);

This will loop through your Arrayand check the coupon_codefor your specified value. If it found something, it will return it in result.

这将遍历您的Array并检查coupon_code您指定的值。如果它找到了一些东西,它会在result.

Note that Array.forEachis available since JavaScript 1.6. You might want to take a look at which browser are supportingit.

请注意Array.forEach从 JavaScript 1.6 开始可用。您可能想看看支持它的浏览器