我有一个json文件,我需要复制到本地对象。我需要使用new Object和Object Assign:
let newObject = new Object();
Object.assign(newObject,MyData.map(data => ({
personal_id : data._id,
idx : data.index,
voiceLines : data.tags
})));
console.log(newObject);
当我检查返回时,我应该只是复制JSON数据并创建一个简单的对象数组。返回值:
'1': {
personal_id: '62bab08c10365bb88f81cdf5',
idx: 1,
voiceLines: [
'non laborum cillum commodo velit culpa commodo',
'nisi aute magna laborum ut cillum velit',
'in veniam ullamco officia aute deserunt ex',
'dolor ullamco aliqua laborum ullamco officia mollit',
'fugiat aliquip nostrud deserunt fugiat veniam veniam',
'culpa eu irure ullamco ea deserunt ullamco',
'labore quis quis enim magna duis cupidatat'
]
},
等等。如何移除顶部的1 ?
我不确定为什么需要new Object()
或Object.assign
,但这里使用这两个:
const MyData =
[
{
_id: '62bab08c10365bb88f81cdf5',
index: 1,
tags: [
'non laborum cillum commodo velit culpa commodo',
'nisi aute magna laborum ut cillum velit',
'in veniam ullamco officia aute deserunt ex',
'dolor ullamco aliqua laborum ullamco officia mollit',
'fugiat aliquip nostrud deserunt fugiat veniam veniam',
'culpa eu irure ullamco ea deserunt ullamco',
'labore quis quis enim magna duis cupidatat'
]
},
{
_id: 'abcdefghijklmnopqrstuvwx',
index: 2,
tags: [
'The grand old duke of York',
'he had ten thousand men',
'he ran them up that hill',
'and made a deal with god',
'to swap our places',
]
}
];
let newObjects = MyData.map(data =>
{
let newObject = new Object();
Object.assign(newObject,
{
personal_id : data._id,
idx : data.index,
voiceLines : data.tags
});
return newObject;
}
);
console.log(newObjects);