简体   繁体   中英

Find common keys of an array and an object in jquery

I have 2 arrays with similar values. What I want is to get the intersection of the arrays and object -

The console.log of first object is -

{  
   "1221":{  
      "oldPrice":{  
         "amount":75
      },
      "basePrice":{  
         "amount":75
      },
      "finalPrice":{  
         "amount":75
      },
      "tierPrices":[  

      ]
   },
   "1222":{  
      "oldPrice":{  
         "amount":75
      },
      "basePrice":{  
         "amount":75
      },
      "finalPrice":{  
         "amount":75
      },
      "tierPrices":[  

      ]
   },
   "1223":{  
      "oldPrice":{  
         "amount":75
      },
      "basePrice":{  
         "amount":75
      },
      "finalPrice":{  
         "amount":75
      },
      "tierPrices":[  

      ]
   },
   "1224":{  
      "oldPrice":{  
         "amount":80
      },
      "basePrice":{  
         "amount":80
      },
      "finalPrice":{  
         "amount":80
      },
      "tierPrices":[  

      ]
   }
}

The console.log of second array is -

[["1222","1223","1224"]]

So basically, I want to get the first object with ids which is equal to second array, which is 1222, 1223, and 1224.

I have tried using inArray but it is not working.

You can use Array.reduce and Object.assign

 let obj = {"1221":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1222":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1223":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1224":{"oldPrice":{"amount":80},"basePrice":{"amount":80},"finalPrice":{"amount":80},"tierPrices":[]}}; let arr = [["1222","1223","1224"]]; let result = arr[0].reduce((a,c) => Object.assign(a, {[c] : obj[c]}), {}); console.log(result); 

Note : In case there is an entry in array which is missing in object , you can update the code to following

 let obj = {"1221":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1222":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1223":{"oldPrice":{"amount":75},"basePrice":{"amount":75},"finalPrice":{"amount":75},"tierPrices":[]},"1224":{"oldPrice":{"amount":80},"basePrice":{"amount":80},"finalPrice":{"amount":80},"tierPrices":[]}}; let arr = [["missing_entry_from_obj", "1222","1223","1224"]]; let result = arr[0].reduce((a,c) => {if(obj[c]) a[c]= obj[c]; return a;}, {}); console.log(result); 

You can use array map to work:

 var a1 = { "1221":{ "oldPrice":{ "amount":75 }, "basePrice":{ "amount":75 }, "finalPrice":{ "amount":75 }, "tierPrices":[ ] }, "1222":{ "oldPrice":{ "amount":75 }, "basePrice":{ "amount":75 }, "finalPrice":{ "amount":75 }, "tierPrices":[ ] }, "1223":{ "oldPrice":{ "amount":75 }, "basePrice":{ "amount":75 }, "finalPrice":{ "amount":75 }, "tierPrices":[ ] }, "1224":{ "oldPrice":{ "amount":80 }, "basePrice":{ "amount":80 }, "finalPrice":{ "amount":80 }, "tierPrices":[ ] } }; var a2 = [["1222","1223","1224"]]; var result = {}; a2[0].map(current=>{ var temp = a1[current]; result[current] = temp; }); console.log(result); 

You could map either the existent object or false, if not found. Then create a new object of the parts.

 var object = { 1221: { oldPrice: { amount: 75 }, basePrice: { amount: 75 }, finalPrice: { amount: 75 }, tierPrices: [] }, 1222: { oldPrice: { amount: 75 }, basePrice: { amount: 75 }, finalPrice: { amount: 75 }, tierPrices: [] }, 1223: { oldPrice: { amount: 75 }, basePrice: { amount: 75 }, finalPrice: { amount: 75 }, tierPrices: [] }, 1224: { oldPrice: { amount: 80 }, basePrice: { amount: 80 }, finalPrice: { amount: 80 }, tierPrices: [] } }, keys = ["1222", "1223", "1224"], result = Object.assign(...keys.map(k => k in object && { [k]: object[k] })); console.log(result); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

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