2 回答

TA貢獻1906條經驗 獲得超10個贊
let obj = {
_attributes: {
name: 'titolo',
description: 'il titolo della sezione'
},
field: [{
_attributes: {
name: 'titolo',
type: 'input',
label: 'titolo',
value: 'titolo'
}
},
{
_attributes: {
name: 'colore',
type: 'input',
select: 'giallo,blu',
label: 'seleziona il colore',
value: 'titolo'
}
}
]
}
obj = { ...obj._attributes, ...obj };
delete obj._attributes;
obj.field = obj.field.map(el => el._attributes);
console.log(obj);

TA貢獻2019條經驗 獲得超9個贊
如何做到這一點:reduce
var obj={ _attributes: { name: 'titolo', description: 'il titolo della sezione' }, field: [ { _attributes: { name: 'titolo', type: 'input', label: 'titolo', value: 'titolo' } }, { _attributes: { name: 'colore', type: 'input', select: 'giallo,blu', label: 'seleziona il colore', value: 'titolo' } } ] };
var result = Object.entries(obj).reduce((acc,[k,v])=>{
if(!Array.isArray(v)){
acc = {...v, ...acc};
} else {
field = v.map(({_attributes})=>_attributes);
acc = {...acc, field}
}
return acc;
},{});
console.log(result);
添加回答
舉報