0

I want to create an array containing objects that have a key data which is an array of values. I am trying to push values into data but it's not working for me.

My expected result looks like this:

array = [
  { data:[3,2,5] },
  { data:[5,2,1] }
]

So I want to fill the data arrays. To do this, I have tried populating the arrays with a for loop:

var array = [];
for(i=0;i<2;i++){
  for(j=0;j<3;j++){
    array[i]["data"][j].push(2);
  }
}

But this is not working for me.

1
  • push is not a value method. Commented Feb 8, 2020 at 21:32

4 Answers 4

2

You can't do this: array[i]["data"][j].push(2) without first initializing array[i].

In your case there's no need to initialize it with an empty array, instead you can simply assign the whole object to each [i]

What you are trying to achieve is an array of objects where each object is of the form {'data': []}

So assume you have your input coming from an array:

const initialData = [[3, 2, 5], [5, 2, 1]];

To get that data in the form you want it, you would do:

const initialData = [[3, 2, 5], [5, 2, 1]];
var array = [];
for(i=0;i < initialData.length; i++){
  array[i] = {'data':initialData[i]};   
}
    
console.log(array)

And here is a neater approach using some of Array.prototype's methods which I recommend checking out:

const initialData = [[3, 2, 5], [5, 2, 1]];

const array = initialData.map(elem => {
  return {
    'data': elem
  };
})

console.log(array)

And if you want neaterer you can go with mplungjan's comment

Sign up to request clarification or add additional context in comments.

2 Comments

No need for the return as long as your wrap the object const array = initialData.map(elem => ({ 'data': elem }));
@mplungjan, true that, but since the OP seems not to have a good grasp on array methods just yet, I thought I'd keep it as readable as possible
1

You can initialize the array with: const array = [...Array(2)].map(() => ({ data: [] }));

Then you'll be able to access it with:

for(i=0; i<2; i++){
  for(j=0;j<3;j++){
    array[i].data[j] = 2;
  }
}

Comments

0

Take a look at these changes, which modify your existing code. For an improved method, see the bottom

Your Code

var array = [];
for(i=0;i<2;i++){
  array.push({data:[]})        // create your object
  for(j=0;j<3;j++){
    array[i]["data"].push(2);  // you don't need '[j]' because the result of 'data' key is an array, so just push onto it
  }
}

console.log(array)

Alternative Method

let arr = [...new Array(2)].map(el=>({ data: new Array(3).fill(2) }));

console.log(arr)

Comments

0

You could do something like this:

let items = 2
let arr = Array.from({ length: items }, (item) => item = { data: [] })
for (let i = 0; i < items; i++) {
  for (let j = 0; j < 3; j++) {
    let val = 2 // replace value here
    arr[i].data.push(val)
  }
}

console.log(arr);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.