How to create a list of objects in JavaScript

I have an array of pairs of values somewhat like this:

 let myarr = [[1, 'a'],
              [2, 'b'],
              [3, 'c']] 

and one like this:

let otherArr = [5,6,7]

and I would like to convert them to an object which would be of the form:

{
   "data":[
      {
         "id":5,
         "pair":[
            1,
            "a"
         ]
      },
      {
         "id":6,
         "pair":[
            2,
            "b"
         ]
      },
      {
         "id":7,
         "pair":[
            3,
            "c"
         ]
      }
   ]
}

As a first attempt

I tried to use a for loop and tried to create a list of the pair keys like this

for (let pair = 0; pair < myarr.length; pair++) {
  myobj[pair].pair = myarr[pair]

and I get an error stating TypeError: Cannot set property 'pair' of undefined

Is there a efficient way to create a object like the example above Thanks in advance

The reason your for loop fails is because you need to instantiate an object before you can set properties on it. For example, you could use Array.push to iterate over your array and create new objects at each index.

const myobj = { data: [] };
for (let index = 0; index < myarr.length; index++) {
    myobj.data.push({ pair: myarr[index], id: otherArr[index]})
}

A shorter way to write the above, assuming that myarra and otherArr will always have the same length, would be to use Array.map to iterate over your first array and return a new one of the same length.

const myobj = { data: myArra.map((pair, index) => ({ pair, id: otherArr[index] }) }

how to create list of objects in javascript, Pretty straight forward approach to creating an array of objects. var employees = []; for (var i = 0; i < 10; i++) { employees.push({ Column1:� how to create list of objects in javascript. Ask Question Asked 7 years, 3 months ago. Pretty straight forward approach to creating an array of objects.

Try this :

let otherArr = [5,6,7];

let myarr = [[1, 'a'], [2, 'b'], [3, 'c']];

let obj = { "data":[] };

otherArr.map((item, index) => {
	obj.data.push({
  	"id": item,
    "pair": myarr[index]
  })
});

console.log(obj);

JavaScript Objects, This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of� This is the easiest way to create a JavaScript Object. Using an object literal, you both define and create an object in one statement. An object literal is a list of name:value pairs (like age:50) inside curly braces {}. The following example creates a new JavaScript object with four properties:

You can do something like below

 let myarr = [[1, 'a'],
              [2, 'b'],
              [3, 'c']];
let otherArr = [5,6,7];

let data = myarr.map((i,idx) => {

    return {
        "id": otherArr[idx],
        "pair": i
    };

});

console.log({data});

JavaScript Arrays, Objects allow us to group values—including other objects—to build more It is called an array and is written as a list of values between square brackets,� To create an object, use the new keyword with Object () constructor, like this: const person = new Object(); Now, to add properties to this object, we have to do something like this: person.firstName = 'testFirstName'; person.lastName = 'testLastName'; You might have figured that this method is a bit longer to type.

Data Structures: Objects and Arrays :: Eloquent JavaScript, in this JS practice session and see how to create a JavaScript array of object literals. 1:40 First, a function that returns a list of the names of all of the products � But sometimes you need to create objects and their arrays manually. Like I did when I was creating this e-shop: Considering each category list item looks like this in HTML: I didn't want to have this code repeated 12 times, which would make it unmaintainable. Creating an array of objects. But let's get back to cars. Let's take a look at this

Create a JavaScript Array of Objects, The head represents the node one past the end of the list and has no value . Returns an object with each property name and value corresponding to the entries in Used by JSON.stringify to create a JSON representation of the collection. Arrays are a special type of objects. The typeof operator in JavaScript returns "object" for arrays. But, JavaScript arrays are best described as arrays. Arrays use numbers to access its "elements". In this example, person[0] returns John:

List, For example, let's create an object named myCar and give it written using an object initializer, which is a comma-delimited list of zero or more� In JavaScript, objects penetrate almost every aspect of the language. So we must understand them first before going in-depth anywhere else. An object can be created with figure brackets {…} with an optional list of properties.

Comments
  • So just to point out logic flaws, your stated desired json consists of an object with a single key of "data". Where is that part in your logic?
  • Look up how to zip two objects
  • (That was a suggestion, not an order)