Tester
Tester

Reputation: 157

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.

Upvotes: 0

Views: 644

Answers (3)

Nikhil Aggarwal
Nikhil Aggarwal

Reputation: 28455

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);

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386654

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; }

Upvotes: 0

protoproto
protoproto

Reputation: 2099

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);

Upvotes: 0

Related Questions