如何通过与其他对象进行比较来修改和替换对象的键?
How to modify & replace the key an object by comparing it with other objects?
我有一个对象数组,其中包含一些 id 作为键。
const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]
我想将 sampleObj1 键与 sampleObj2 id 值进行比较,然后需要创建新的对象,如下所示。
const newObjByComparing = {test1:[{},{}], test2:[{},{}], test3:[{},{}]} //desired result
我试过使用 for 循环,但无法获得所需的比较值。
const fromRdx = sampleObj2;
const fromApi = sampleObj1;
const keys = Object.keys(fromApi);
const newObjAfterComparision = {};
for (let i in fromRdx) {
newObjAfterComparision[fromRdx[i]] = fromApi[keys[i]];
fromApi[fromRdx[i]] = fromApi[keys[i]];
delete fromApi[keys[i]];
}
console.log(newObjAfterComparision)
以下可能是实现所需 objective 的一种可能解决方案:
代码段
const transformObj = (obj, arr) => arr.reduce((f, i) => ({
...f,
[i.name]: obj[i.id]
}), {});
const sampleObj = {
"0011": [{}, {}],
"0022": [{}, {}],
"0033": [{}, {}]
};
const sampleArr = [{
id: "0011",
name: "test1"
}, {
id: "0022",
name: "test2"
}, {
id: "0033",
name: "test3"
}];
console.log(transformObj(sampleObj, sampleArr));
解释
- 使用
.reduce()
迭代sampleArr
- 为每个元素填充一个结果对象(名为
f
)
- 对象的键将是元素的
name
,值将是 sampleObj
中的值(其中键是元素的 id
)
使用 map
和 Object.fromEntries
的另一种方法
const sampleObj1 = {'0011':[{},{}], '0022':[{}, {}], '0033':[{},{}]}
const sampleObj2 = [{id:'0011', name:'test1'}, {id:'0022', name:'test2'}, {id:'0033', name:'test3'}]
let y = Object.fromEntries(sampleObj2.map(({id,name}) => [name,sampleObj1[id]]))
console.log(y)
我有一个简单的解决方案给你
const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]
const keys = Object.keys(sampleObj1);
const newObjByComparing = sampleObj2.reduce((acc, cur, index) => {
acc[cur.name] = sampleObj1[keys[index]];
return acc;
}, {});
我有一个对象数组,其中包含一些 id 作为键。
const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]
我想将 sampleObj1 键与 sampleObj2 id 值进行比较,然后需要创建新的对象,如下所示。
const newObjByComparing = {test1:[{},{}], test2:[{},{}], test3:[{},{}]} //desired result
我试过使用 for 循环,但无法获得所需的比较值。
const fromRdx = sampleObj2;
const fromApi = sampleObj1;
const keys = Object.keys(fromApi);
const newObjAfterComparision = {};
for (let i in fromRdx) {
newObjAfterComparision[fromRdx[i]] = fromApi[keys[i]];
fromApi[fromRdx[i]] = fromApi[keys[i]];
delete fromApi[keys[i]];
}
console.log(newObjAfterComparision)
以下可能是实现所需 objective 的一种可能解决方案:
代码段
const transformObj = (obj, arr) => arr.reduce((f, i) => ({
...f,
[i.name]: obj[i.id]
}), {});
const sampleObj = {
"0011": [{}, {}],
"0022": [{}, {}],
"0033": [{}, {}]
};
const sampleArr = [{
id: "0011",
name: "test1"
}, {
id: "0022",
name: "test2"
}, {
id: "0033",
name: "test3"
}];
console.log(transformObj(sampleObj, sampleArr));
解释
- 使用
.reduce()
迭代sampleArr
- 为每个元素填充一个结果对象(名为
f
) - 对象的键将是元素的
name
,值将是sampleObj
中的值(其中键是元素的id
)
使用 map
和 Object.fromEntries
const sampleObj1 = {'0011':[{},{}], '0022':[{}, {}], '0033':[{},{}]}
const sampleObj2 = [{id:'0011', name:'test1'}, {id:'0022', name:'test2'}, {id:'0033', name:'test3'}]
let y = Object.fromEntries(sampleObj2.map(({id,name}) => [name,sampleObj1[id]]))
console.log(y)
我有一个简单的解决方案给你
const sampleObj1 = {0011:[{},{}], 0022:[{}, {}], 0033:[{},{}]}
const sampleObj2 = [{id:0011, name:'test1'}, {id:0022, name:'test2'}, {id:0033, name:'test3'}]
const keys = Object.keys(sampleObj1);
const newObjByComparing = sampleObj2.reduce((acc, cur, index) => {
acc[cur.name] = sampleObj1[keys[index]];
return acc;
}, {});