我有一个具有具体结构的物体。我想创建另一个具有其他结构的东西.... map()
之类的东西const Object: Icon = {
Laughing: {
iconClass: 'emoticon-3',
name: 'Laughing :D',
dataText: ':D',
},
Surprise: {
iconClass: 'emoticon-4',
name: "Surprise, No you di'int' :O",
dataText: ':O',
}
.......
}
我想将其映射到其他对象结构:
const Object2: Type2 = {
:D: {
iconClass: 'emoticon-3',
name: 'Laughing :D',
},
:O: {
iconClass: 'emoticon-4',
name: "Surprise, No you di'int' :O",
}
.......
}
答案 0 :(得分:1)
您可以使用Object.values()
和reduce
。使用destructuring将66 BE 00000000 mov esi,0x0
作为一个单独的变量,并将其余属性分配给dataText
变量,如下所示:rest
{ dataText, ...rest }
按照@jo_va的建议,您还可以隐式地从const obj = {
Laughing: {
iconClass: 'emoticon-3',
name: 'Laughing :D',
dataText: ':D',
},
Surprise: {
iconClass: 'emoticon-4',
name: "Surprise, No you di'int' :O",
dataText: ':O',
}
}
const newObj = Object.values(obj).reduce((acc, { dataText, ...rest }) => {
acc[dataText] = rest;
return acc
}, {})
console.log(newObj)
返回:
reduce
答案 1 :(得分:1)
您可以使用array#map
创建对象数组,然后使用Object.assign()
创建单个对象。
const obj = { Laughing: { iconClass: 'emoticon-3', name: 'Laughing :D', dataText: ':D', }, Surprise: { iconClass: 'emoticon-4', name: "Surprise, No you di'int' :O", dataText: ':O', } },
result = Object.assign(...Object.values(obj).map(({dataText, ...o}) => ({[dataText] : o})));
console.log(result);