为什么在复制JSON数据文件之后,对象分配我的对象数组?

发布于 2025-02-11 09:59:42 字数 768 浏览 2 评论 0原文

我有一个JSON文件,需要将其复制到本地对象。 我需要使用新对象和对象分配:

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?

I have a json file that I need to copy to my local object.
I need to use new Object and 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);

When I check the return, I should have just copied the JSON data and make a simple array of objects.
The return though :

'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'
]
},

And so on with the other objects.
How do I remove the 1 at the top?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟沫凡尘 2025-02-18 09:59:42

我不确定为什么您需要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);

I'm not sure why you need new Object() or Object.assign, but this uses both of those:

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文