简体   繁体   中英

Parse this Json response and loop through the array in JavaScript

There are many post about this but I can't figure this one out. This is the json response from an Ajax function:

var obj = {
    "3901": 10,
    "3900": 3,
    "3902": 9,
    "3899": 2,
    "3274": 4,
    "3257": 9.5,
    "3883": 12,
    "3881": "12",
    "3876": 3,
    "3267": 14,
    "3258": 2.5,
    "3260": 13.5,
    "3259": 6.5,
    "3264": 4.5,
    "3268": 2,
    "3273": 5.5,
    "3266": 17,
    "3270": 9,
    "3271": 8,
    "3275": 2,
    "3263": 2.5,
    "3261": 2.5,
    "3265": "37",
    "3281": 3,
    "3277": 7.5,
    "3278": 0.5,
    "3276": 7,
    "3898": 8,
    "3891": 7,
    "3293": 1,
    "3895": 1,
    "3256": 2,
    "3903": 10,
    "3840": 2,
    "3886": 11,
    "3884": 8,
    "3872": 2,
    "3874": 4,
    "3284": 1.5,
    "3302": 1.25,
    "3304": 5,
    "3306": 2,
    "3329": 1.5,
    "3330": 2,
    "3333": 6,
    "3335": 7.5,
    "3327": 1,
    "3934": 8,
    "3935": 9,
    "3939": 1,
    "3933": 3,
    "3937": 1,
    "3322": 3.5,
    "3890": 1,
    "3878": 5,
    "3880": 4,
    "3879": 1,
    "3889": 2,
    "3852": 2,
    "3877": 2
}

I have a this of ids: 3902, 3883, 4567 and 3878

All I need is to loop through the those 4 ids and check if those are in the json response and if they are get the value associated to it and if not return 0. For example:

3902 will return 9 and 4567 will return 0

Thanks.

You can put all the ids into an array called ids , then use .map() on that array. For each id within the array, you can look it up in your object using obj[id] . If it doesn't exist, it will return undefined . If this occurs, you can use a default of 0 by using an || :

See example below:

 const obj = {"3901":10,"3900":3,"3902":9,"3899":2,"3274":4,"3257":9.5,"3883":12,"3881":"12","3876":3,"3267":14,"3258":2.5,"3260":13.5,"3259":6.5,"3264":4.5,"3268":2,"3273":5.5,"3266":17,"3270":9,"3271":8,"3275":2,"3263":2.5,"3261":2.5,"3265":"37","3281":3,"3277":7.5,"3278":0.5,"3276":7,"3898":8,"3891":7,"3293":1,"3895":1,"3256":2,"3903":10,"3840":2,"3886":11,"3884":8,"3872":2,"3874":4,"3284":1.5,"3302":1.25,"3304":5,"3306":2,"3329":1.5,"3330":2,"3333":6,"3335":7.5,"3327":1,"3934":8,"3935":9,"3939":1,"3933":3,"3937":1,"3322":3.5,"3890":1,"3878":5,"3880":4,"3879":1,"3889":2,"3852":2,"3877":2}; const ids = [3902, 3883, 4567, 3878]; const res = ids.map(id => obj[id] || 0); console.log(res);

var jsonObject = {3256: 2, 3257: 9.5, 3258: 2.5, 3259: 6.5, 3260: 13.5, 3261: 2.5, 3263: 2.5, 3264: 4.5, 3265: "37", 3266: 17, 3267: 14, 3268: 2, 3270: 9, 3271: 8, 3273: 5.5, 3274: 4, 3275: 2, 3276: 7, 3277: 7.5, 3278: 0.5, 3281: 3, 3284: 1.5, 3293: 1, 3302: 1.25, 3304: 5, 3306: 2, 3322: 3.5, 3327: 1, 3329: 1.5, 3330: 2, 3333: 6, 3335: 7.5, 3840: 2, 3852: 2, 3872: 2, 3874: 4, 3876: 3, 3877: 2, 3878: 5, 3879: 1, 3880: 4, 3881: "12", 3883: 12, 3884: 8, 3886: 11, 3889: 2, 3890: 1, 3891: 7, 3895: 1, 3898: 8, 3899: 2, 3900: 3, 3901: 10, 3902: 9, 3903: 10, 3933: 3, 3934: 8, 3935: 9, 3937: 1, 3939: 1}

var ids = [3902, 3883, 4567, 3878];

for(var i =0; i < ids.length; i++)
{  
   if(temp1.hasOwnProperty(ids[i])) //to check propery exist in JSON object
   { 
      console.log(temp1[ids[i]]) //to read value from JSON object
    }
 }

I think you already have a valid JSON here.

let data = {"3901":10,"3900":3,"3902":9,"3899":2,"3274":4,"3257":9.5,"3883":12,"3881":"12","3876":3,"3267":14,"3258":2.5,"3260":13.5,"3259":6.5,"3264":4.5,"3268":2,"3273":5.5,"3266":17,"3270":9,"3271":8,"3275":2,"3263":2.5,"3261":2.5,"3265":"37","3281":3,"3277":7.5,"3278":0.5,"3276":7,"3898":8,"3891":7,"3293":1,"3895":1,"3256":2,"3903":10,"3840":2,"3886":11,"3884":8,"3872":2,"3874":4,"3284":1.5,"3302":1.25,"3304":5,"3306":2,"3329":1.5,"3330":2,"3333":6,"3335":7.5,"3327":1,"3934":8,"3935":9,"3939":1,"3933":3,"3937":1,"3322":3.5,"3890":1,"3878":5,"3880":4,"3879":1,"3889":2,"3852":2,"3877":2};

let arr = [3902, 3883, 4567 , 3878];

let ans = arr.map( i => {
  if(data[i] === undefined) {
   console.log(0);
   return 0;
  }
  else {
   console.log(data[i]);
   return data[i];
 }
});

ans is the required array.

You can use a foreach

 j={"3901":10,"3900":3,"3902":9,"3899":2,"3274":4,"3257":9.5,"3883":12,"3881":"12","3876":3,"3267":14,"3258":2.5,"3260":13.5,"3259":6.5,"3264":4.5,"3268":2,"3273":5.5,"3266":17,"3270":9,"3271":8,"3275":2,"3263":2.5,"3261":2.5,"3265":"37","3281":3,"3277":7.5,"3278":0.5,"3276":7,"3898":8,"3891":7,"3293":1,"3895":1,"3256":2,"3903":10,"3840":2,"3886":11,"3884":8,"3872":2,"3874":4,"3284":1.5,"3302":1.25,"3304":5,"3306":2,"3329":1.5,"3330":2,"3333":6,"3335":7.5,"3327":1,"3934":8,"3935":9,"3939":1,"3933":3,"3937":1,"3322":3.5,"3890":1,"3878":5,"3880":4,"3879":1,"3889":2,"3852":2,"3877":2} se=[3902, 3883, 4567, 3878] res=[] se.forEach(s=>{ res.push({[s]:j[s]||0}) }) console.log(res)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM