Home > Net >  Why does Object.assign numbers my array of objects after copying a JSON data file?
Why does Object.assign numbers my array of objects after copying a JSON data file?

Time:06-29

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?

CodePudding user response:

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

  • Related