What causes my array of objects to be assigned numbers when copying a JSON data file using Object.assign?

I have a JSON file that I need to transfer to my local object using new Object and Object Assign:

let newObj = new Object(); 
Object.assign(newObj, MyData.map(data => ({
personal_id : data._id,
idx : data.index,
voiceLines : data.tags
})));

console.log(newObj);

After checking the returned result, it appears as though the JSON data was successfully copied into a simple array of objects. However, the output includes extra numerical keys at the beginning like '1': {...}. How can I remove these numbers?

Answer №1

It's interesting that both new Object() and Object.assign are used in this example:

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 newObj = new Object();
  Object.assign(newObj, 
    {
      persId : data._id,
      idx : data.index,
      voiceLines : data.tags
    });
   return newObj;
}
);

console.log(newObjects);

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

When using Laravel and Vue in Javascript, the Array Length function may sometimes return an undefined

I'm currently immersed in a project utilizing Vue + Laravel, where I rely on axios for handling AJAX requests to fetch data from controllers and showcase it on the Javascript side. However, when employing nested for loops, I encounter some peculiar be ...

What is causing Node to mistake my MongoDB model for a module?

I've set up a basic MERN project with a UserModel included. import mongoose from "mongoose" const UserSchema = new mongoose.Schema({ name: String, email: String, password: String }) const UserModel = mongoose.model('Users', ...

What is the best way to assign an ajax array to a ColdFusion variable?

I've encountered an issue while working on HTML code that involves filling in special machine labels. Users need to input the number of lines, text size, and the text for each line they want in the resulting table. Currently, I'm seeking advice ...

CSS and JavaScript Nav Menu Collapse (No Bootstrap)

I have written a navbar code using pure HTML/SASS, but I am facing a challenge in adding a collapse element to the navigation bar. Despite trying various solutions from Stack Overflow, I still haven't found one that works for me. Therefore, I am rea ...

Using AngularJS to show/hide elements within a colgroup tag

Looking to create a dynamic table allowing users to hide certain columns. Wondering if it's possible to use ng-show with colgroup or col tags instead of adding ngshow to each cell individually. Struggling to make it work... <colgroup ng-repeat="mt ...

When the jGrowl function is triggered, it disrupts the functionality of the thickbox in conjunction with UpdatePanel

Whenever a link is clicked, I trigger a thickbox like this: <a href="createContact.aspx?placeValuesBeforeTB_=savedValues&TB_iframe=true&height=400&width=550&modal=true" title="Add a new Contact" class="thickb ...

JavaScript-enhanced HTML form validation

I encountered a small glitch while working on simple form validation with JavaScript. I tried to catch the issue but have been unable to do so. Here is the code snippet, where the problem lies in the fact that the select list does not get validated and I ...

Using the function to show content by calling its assigned ID

<script type="text/javascript"> function selectRow(id){ $.get("http://inactive/test.php?id=" + id, function(data,status){ return data; }); } </script> <script type="text/javascript"> $("tr").click(function() { window.l ...

Determine the percentage between two different dates

I am looking to calculate the percentage difference between two dates, considering only hours for scaling. For example: 22-08-2017 09:00 as the start date, 30.09.2017 22:00 as the finish date, Today's date is 01.09.2017. When I check the system toda ...

What is the most effective way to retrieve 'this' within an object method that is being used as a callback function?

I am currently working on a word game project to enhance my understanding of JavaScript, especially since I am new to it. To facilitate user interaction, I am utilizing the NPM package prompt (https://www.npmjs.com/package/prompt). Coming from an OOP back ...

Changing Underscores in ES6 Techniques

My current project is built using AngularJS, Redux, and Underscore. Within this project, I have the following code snippet: const selectedBroker = _.findWhere(state.brokers, { brokerId: action.payload }); return state.merge({ selectedBroker, sel ...

What methods are available to modify the colors in an Apex bar chart?

Currently, I am in the process of constructing a bar chart using react, mui, and apex-chart. One specific requirement I have is to modify the colors of the bars displayed on the chart. Despite my efforts in attempting various solutions, I have been unsucce ...

Error: result.push is not a function

I've encountered an issue with two functions I have. Both of them generate a range from a starting integer to an ending integer, inclusive. Here's how they're defined: function createRange(start, end) { var result = []; for (var i = sta ...

The File is not being successfully sent to the controller in MVC due to AJAX returning an undefined value

I recently created an AJAXUpload method within a cshtml file in my C# MVC project: function AjaxUpload(url, method, data, successFunction, errorFunction, skipErrorDlg) { $.ajax({ contentType: false, processData: false, url: url ...

Incorporating a feature that displays one show at a time and includes a sliding animation into an

Hey there! I have this show/hide script on my website that's been working well. However, there are a couple of things I need help with. I want to modify the script so that only one div can be shown at a time. When a div appears, I'd love for it ...

The function screen.getByText is not available in this context

My experience with jest and react-testing-library has been smooth for the most part, but I encountered some challenges when transitioning to the screen > getByText/etc testing method. Test describe('test the dashboard when loaded', () => { ...

Update the table that includes a php script

I have a piece of PHP code embedded within a table tag that displays text from a database. I am looking for a way to automatically refresh this table every minute with updated content from the database, without refreshing the entire page. While I have co ...

Ways to transfer a value from a JavaScript file to a PHP file?

Is there a way to transfer data from a JavaScript file to a PHP file? Code: var year = (year != null) ? year : '".$this->arrToday["year"]."'; var month = (month != null) ? month : '".$this->ConvertToDecimal($this>arrTod ...

Can the background color be eradicated while dragging a png image?

Currently, I am working on developing a chess game in react-js using react-dnd. My goal is to create draggable and droppable PNG images that can be moved between various div elements representing the board squares. I attempted to set the opacity of the i ...

Why is my Angular app displaying outdated data from Firebase when navigating between pages?

Currently, I am dealing with an Angular 9 application that is hosted on Firebase and utilizes Firestore for its data storage. There is a perplexing issue that I haven't been able to figure out completely despite simplifying the app extensively. I will ...