最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Search JSON Array for a String and Retrieve Object that Contains it as a Value - Stack Overflow

programmeradmin1浏览0评论

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?

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.

In short, I just need to get the id of the Coupon with coupon_code: 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 :)

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?

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.

In short, I just need to get the id of the Coupon with coupon_code: 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 :)

Share Improve this question edited Dec 15, 2014 at 21:12 Zo72 15.3k18 gold badges74 silver badges105 bronze badges asked Jun 5, 2013 at 17:52 ac360ac360 7,83514 gold badges55 silver badges95 bronze badges 2
  • I think for-loop will do: stackoverflow.com/questions/9329446/… – Igor Dymov Commented Jun 5, 2013 at 17:55
  • 3 By the time you're searching it, it's not JSON anymore. It's just an array. – T.J. Crowder Commented Jun 5, 2013 at 18:04
Add a comment  | 

3 Answers 3

Reset to default 9

You just loop through the array and look. There are lots of ways to do that in 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#some method (which is one that can be "shimmed" for browsers that don't yet have it, search for "es5 shim"):

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

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')
var result = null;
Objects.forEach(function(obj, i){
    if(obj.cupon_code == 'COUPON1'){
        return result = obj;
    }
});
console.log(result);

This will loop through your Array and check the coupon_code for your specified value. If it found something, it will return it in result.

Note that Array.forEach is available since JavaScript 1.6. You might want to take a look at which browser are supporting it.

发布评论

评论列表(0)

  1. 暂无评论