I am trying to put a value to an array that is inside another I have tried to use the push method but without success my code is as follows
var data = [
{
x: [resultado[0][0],resultado[1][0]],
y: [resultado[0][1],resultado[1][1]],
type: 'bar'
}
];
the question is that I want to introduce the next value inside X or Y with push I have tried this
data.push.x(resultado[2][0])
But without any success
If someone can help me I would really appreciate it......
Your data variable is an Array, specifically an Array of objects.
To access the first (and only) element it contains, you must start with
data[0]
, from there you can access the object inside, and specifically you want to access its variable X, which leaves us withdata[0].x
.At this point you are selecting the Array that contains the variable X of the first object of your Array data. That's when you should do the push():
data[0].x.push(resultado[2][0]);
data is an array of objects, and you see its properties "x" and "y", they have an array of 2 results if I'm not mistaken, right?
What you are needing, is to access the Zero position of data, which you show in your code portion, access the property by the nomenclature of the point, and as you know, that is an array of the matrix that you are using
Be careful because even though javascript is loosely typed, and permissive when it comes to starting an empty array, it has to know what position the one you want to access is in.