Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript array push subarray

Each newdata[x][0]] looks like this -> [95, 152, 174, 197, 261] but when I do newgooddata.push([newdata[x][0]]) twice (x=0 and 1) I get NEWDATA

I want it to be:

THE STRUCTURE I WANT

I seem to be adding them wrong. Some help , with an explanation?


1 Answers

You are putting newdata[x][0] into an array before pushing.

newgooddata.push([newdata[x][0]])  // bad

newgooddata.push(newdata[x][0])    // good

The extra [] around newdata[x][0] creates a new array containing one element: newdata[x][0].

like image 161
Caleb Avatar answered May 10 '26 10:05

Caleb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!