[英]Find common keys of an array and an object in jquery
I have 2 arrays with similar values. 我有2个具有相似值的数组。 What I want is to get the intersection of the arrays and object - 我想要的是获取数组和对象的交集-
The console.log
of first object is - 第一个对象的console.log
是-
{
"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 - 第二个数组的console.log
是-
[["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. 因此,基本上,我想获取具有与第二个数组相等的id的第一个对象,即1222、1223和1224。
I have tried using inArray
but it is not working. 我曾尝试使用inArray
但无法正常工作。
You can use Array.reduce and Object.assign 您可以使用Array.reduce和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 注意 :如果object
缺少array
中的一个条目,您可以将代码更新如下:
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. 您可以映射现有对象或false(如果找不到)。 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; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.