我有一个对象:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
如何将id
组中的每个对象添加到与name prop
中相同的值以获取此信息:
const obj2 = {
app: {
groups: [{ name: 'name', values: 'values', id: 'name' }, { name: 'name1', values: 'values1', id: 'name1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values', id: 'name2' }, { name: 'name7', values: 'values4', id: 'name7' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5', id: 'name5' }, { name: 'name4', values: 'values4', id: 'name4' }]
}
};
答案 0 :(得分:2)
可变版本::如果可以修改输入对象,请两次使用Array.forEach()
:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
Object.keys(obj).forEach(key => {
obj[key].groups.forEach(x => x.id = x.name);
});
console.log(obj);
不可变的版本::如果您不想修改输入对象,请使用Array.reduce()
和散布运算符从内部对象复制属性:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
const result = Object.entries(obj).reduce((acc, [key, val]) => {
acc[key] = { groups: val.groups.map(x => ({ ...x, id: x.name })) };
return acc;
}, {});
console.log(result);
在两种情况下,您都需要使用Object.keys()
遍历键,或者直接使用Object.entries()
遍历条目。
答案 1 :(得分:0)
您可以尝试使用for..in
和forEach
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (let item in obj) {
obj[item].groups.forEach(o => o.id = o.name)
}
console.log(obj)
答案 2 :(得分:0)
遍历对象,然后循环遍历内部对象中的数组:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (app in obj) {
obj[app].groups.forEach(innerObj => {
innerObj.id = innerObj.name;
});
}
console.log(obj);
答案 3 :(得分:0)
迭代Object.keys
并使用map
,如下所示:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
function addIdProp(object) {
let result = object;
Object.keys(result).forEach(key => {
result[key].groups = result[key].groups.map(({ name, values }) => { return { name, values, id: name}});
});
return result;
}
const obj2 = addIdProp(obj);
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0 }
ES5语法:
var obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
function addIdProp(object) {
var result = object;
Object.keys(result).forEach(function(key) {
result[key].groups = result[key].groups.map(function({ name, values }) { return { name: name, values: values, id: name}});
});
return result;
}
var obj2 = addIdProp(obj);
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0 }
答案 4 :(得分:0)
这怎么办?
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1' }]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
for (let k in obj) {
if (obj.hasOwnProperty(k)) {
obj[k].groups = obj[k].groups.map((g) => {
return { ...g, id: g.name}
})
}
}
console.log(obj);
答案 5 :(得分:0)
遍历对象属性并添加其group
数组ID道具,为相关的name
分配两个嵌套的forEach
,如下所示:
const obj = {
app: {
groups: [{
name: 'name',
values: 'values'
}, {
name: 'name1',
values: 'values1'
}]
},
app2: {
groups: [{
name: 'name2',
values: 'values'
}, {
name: 'name2',
values: 'values4'
}]
},
app3: {
groups: [{
name: 'name5',
values: 'values5'
}, {
name: 'name4',
values: 'values4'
}]
}
};
Object.keys(obj).forEach(e => obj[e].groups.forEach(el => el.id = el.name));
console.log(JSON.stringify(obj))
请注意,执行此操作不会创建新对象,而是会修改初始对象。您的用例可能会令人欢迎或灰心。
答案 6 :(得分:0)
您可以这样做:
const obj = {app: {groups: [{name: 'name',values: 'values'}, {name: 'name1',values: 'values1'}]},app2: {groups: [{name: 'name2',values: 'values'}, {name: 'name2',values: 'values4'}]},app3: {groups: [{name: 'name5',values: 'values5'}, {name: 'name4',values: 'values4'}]}};
const obj2 = Object.keys(obj).map(k => ({
[k]: {
groups: obj[k].groups.map(o => {
o.id = o.name;
return o;
})
}
}));
console.log(obj2);
.as-console-wrapper { max-height: 100% !important; top: 0; }
答案 7 :(得分:0)
一种纯粹的功能,不变且独立于范围/闭包的解决方案是将[Object.entries()][1]
的{{1}}缩减为新的obj
结果,如下所示:
obj2
答案 8 :(得分:0)
您必须先遍历对象,然后再像这样在groups数组内进行映射:
const obj = {
app: {
groups: [{ name: 'name', values: 'values' }, { name: 'name1', values: 'values1'}]
},
app2: {
groups: [{ name: 'name2', values: 'values' }, { name: 'name2', values: 'values4' }]
},
app3: {
groups: [{ name: 'name5', values: 'values5' }, { name: 'name4', values: 'values4' }]
}
};
var newObj = {...obj};
for(var key in newObj){
newObj[key] = newObj[key].groups.map(item => ({...item, id: item.name}));
}
console.log(newObj); // here is your new object with id's