![](/img/trans.png)
[英]How to insert new key value pair in array of objects depending on two different keys?
[英]How to compare two array and create new one with different key value pair?
我有两个数组,需要获取特定的键值对并分配给第三个数组,它还会处理重复条目。
第一个数组:
[
{id: 2, name: "HSBC", status: "YES"},
{id: 3, name: "Morgan Stanley", status: "Pending"}
]
第二个数组:
[
{id: 1, name: "Deutsche Bank", category: "EQUITIES"},
{id: 2, name: "HSBC", category: "EQUITIES"},
{id: 3, name: "Morgan Stanley", category: "EQUITIES"},
{id: 4, name: "Credit Suisse", category: "EQUITIES"},
]
从上面的两个数组中,我需要获取名称和状态字段并创建新数组。 如下所示:
[
{ brokerName: "HSBC", statis: "YES"},
{ brokerName: "Morgan Stanley", statis: "Pending"},
{ brokerName: "Deutsche Bank", statis: ""},
{ brokerName: "Credit Suisse", statis: ""},
]
TIA。 堆栈闪电战
let arr1 = [{id: 1, name: "name", status: "YES"}];
let arr2 = [{id: 1, name: "name", category: "EQUITIES"}];
let sumbit = [];
for (let i=0; i<arr1.length; i++) {
let is_there = false;
for (let j=0; j<arr2.length; j++) {
if (arr1[i].name == arr[j].name) {
submit.push({brokerName: arr1[i].name, statis: arr1[i].status});
is_there = true;
}
}
if (!is_there) submit.push({brokerName: arr1[i].name, statis: ""})
}
你可以只写代码:D
这是一个例子:
const array1 = [ {id: 2, name: "HSBC", status: "YES"}, {id: 3, name: "Morgan Stanley", status: "Pending"} ]; const array2 = [ {id: 1, name: "Deutsche Bank", category: "EQUITIES"}, {id: 2, name: "HSBC", category: "EQUITIES"}, {id: 3, name: "Morgan Stanley", category: "EQUITIES"}, {id: 4, name: "Credit Suisse", category: "EQUITIES"}, ]; // returns { name, status } recoreds from any number of arrays function getBrokersDataFromArrays(...arrays) { const allBrokers = new Map(); // collect all the available data for all the brokers arrays.flat().forEach(broker => { if (allBrokers.has(broker.name)) { allBrokers.set(broker.name, { ...allBrokers.get(broker.name), ...broker }); } else { allBrokers.set(broker.name, broker); } }); // return only { name, status } fields from the collected brokers return Array.from(allBrokers.values()).map(broker => ( { name: broker.name, status: broker.status } )); } const brokersData = getBrokersDataFromArrays(array1, array2); console.log(brokersData);
let array1 = [
{id: 2, name: "HSBC", status: "YES"},
{id: 3, name: "Morgan Stanley", status: "Pending"}
]
let array2 = [
{id: 1, name: "Deutsche Bank", category: "EQUITIES"},
{id: 2, name: "HSBC", category: "EQUITIES"},
{id: 3, name: "Morgan Stanley", category: "EQUITIES"},
{id: 4, name: "Credit Suisse", category: "EQUITIES"},
]
// filter data which are present into both array
const result1 = array1.filter(o => array2.some(({ name }) => o.name === name));
// filter data which are present into array1 but not into array2
const result2 = array1.filter((o1) => {
return !array2.some((o2) => {
return o1.name === o2.name;
});
})
// filter data which are present into array2 but not into array1
const result3 = array2.filter((o1) => {
return !array1.some((o2) => {
return o1.name === o2.name;
});
})
// concat all result
let result = (result1.concat(result2)).concat(result3)
let finalArr = []
for (let obj of result) {
finalArr.push({ brokerName: obj.name, status: obj.status ? obj.status : "" })
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.