I have problem with pushing data. I want to prepare data (time,temperature,humidity) for plotting (Dygraphs). But when I´m filling variable data with this one code (see below) I don´t get graph.
for (var i = 0; i < time.length; i++){
var t = new Date (time[i]);
data.push(t);
for(var n = 0; n < 2; n++){
data.push([data_graph[n][i]]);
}
}
But when I leave one for-cycle and manually write nums of arrays (see below), It works and I get graph.
for (var i = 0; i < time.length; i++){
var t = new Date (time[i]);
data.push([t,data_graph[0][i],data_graph[1][i]]);
}
I got idea to use temporary variable, but also with no success.
for (var i = 0; i < time.length; i++){
var data_temporary = [];
var t = new Date (time[i]);
for(var n = 0; n < 2; n++){
data_temporary.push([data_graph[n][i]]);
}
data.push([t,data_temporary]);
}
So my question is...where could be a problem?
Thanks in advance for answers.
Yes, your three code snippets generate three different data structures:
[t, [datagraph…], [datagraph…], t, [datagraph…], [datagraph…], …]
[[t, datagraph…, datagraph…], [t, datagraph…, datagraph…], …]
[[t, [[datagraph…], [datagraph…]]], [t, [[datagraph…], [datagraph…]]], …]
Too often you pushed one-element-arrays, btw.
So if you want struc#2 generated by a loop, use
for (var i=0; i<time.length; i++) {
var t = new Date (time[i]);
var temp = [t]; // or temp=[]; temp.push(t);
for (var j=0; j<data_graph.length; j++) // or j<2 if that's certain
temp.push(data_graph[j][i]);
data.push(temp);
}
Each call to push() creates a new element in your data array. So, in your first example, you are passing 3 objects on each iteration of the outer for loop, and in the third example you are passing an object that consists of time and an array of two values. But the dygraph script apparently expects objects consisting of three elements, so your second example works.
The second (working) version creates a two dimension array with time.length elements in the first dimension each containing three elements [t, x, y], as required.
In the first version you are creating a one-dimensional array [t0, [x0], [y0], t1, [x1], [y1], ...].
Your third version doesn't work because whilst you correctly create time.length elements in the first dimension, the elements themselves are [t, [[x], [y]]], and not [t, x, y].
Related
I have an multi-dimension array that i'm iterating through. Is there a way to put the contents of the array into a new multi-dimension array creating a new MDA? For example, the following code puts all indices of the original array into the new candies array if there's a match. Currently i'm doing
candies.push([product[0],product[1], etc...]);
I'm just trying to see if there's a faster/cleaner way to get that in there.
I tried:
candies.push(product);
but that didn't work. Here's the code i'm currently using
var sel = 'candy';
var candies = [];
for(var i = 1; i < products.length; i++) {
var product = products[i];
for(var j = 0; j < product.length; j++) {
if(sel==product[11]){
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
}
break;
}
}
A cleaner way of writing below line:
candies.push([product[0],product[1],product[2],product[3],product[4],product[5],product[6],product[7],product[8],product[9],product[10],product[11],product[12]]);
is:
candies.push(product.slice(0,13)]);
Sample example JSFiddle is here.
Best of luck.
(Mark this as answer if it serves your need.)
Note: I am not asking how to append data to an array!
Rather my problem is that I want to append items to each and every element of an array.
Here is a part of my code:
dataset=[];
var xpoints=["Jan","Feb","Mar","Apr","May"];
var ypoints=[10,20,30,40,50];
for (var i = 0; i < xpoints.length; i++) {
dataset.push({
x : xpoints[i],
y : parseFloat(ypoints[i])
});
}
The array so far would be as below:
dataset[0] - {x:Jan,y:10}
dataset[1] - {x:Feb,y:20}
dataset[2] - {x:Mar,y:30}
dataset[3] - {x:Apr,y:40}
dataset[4] - {x:May,y:50}
So far there is no problem...
But if now i have another array (Suppose that it is of the same length), I want to append the new array's elements into my existing array such that my output would be as follows:
var zpoints=["a","b","c","d","e"];
/*
Do something
*/
Required Output:
dataset[0] - {x:Jan,y:10,z:a}
dataset[1] - {x:Feb,y:20,z:b}
dataset[2] - {x:Mar,y:30,z:c}
dataset[3] - {x:Apr,y:40,z:d}
dataset[4] - {x:May,y:50,z:e}
If I do:
for (var i = 0; i < dataset.length; i++) {
dataset.push({
z:zpoints[i]
});
}
it would append it as different elements in the dataset array, which is not what I am looking for.
Is the required output achieveable using JavaScript? How?
What if I want to add multiple objects to the dataset array but I do not know the number of objects to be added while compiling?
Suppose that there can be multiples arrays:
z1=["a","b","c","d","e"];
z2=["l","m","n","o","p"];
z3=...
.
.
and so on.. and the number is unknown until runtime.
I want to do something like this:(invalid code)
for(var j=0;j<length;j++) //Length will be known only during runtime
for (var i = 0; i < dataset.length; i++) {
dataset[i].z[j] = zpoints[i]; //z[j] is invalid!!
}
I need to name the objects dynamically somehow. Is there a way to achieve this?
It's rather simple:
for (var i = 0; i < dataset.length; i++) {
dataset[i].z = zpoints[i];
}
A .push call will always append more entries to the array; in this case you want to modify the existing ones.
You need to simply add new property z to existing object:
var l = zpoints.length;
while(l --)
dataset[l].z = zpoints[l];
Using JavaScript I would like to offset the elements in a typed array so that if the original array had values of 0 to 99, the new array would start at 10 and go to 99 leaving 10 empty elements for new data.
So if the original array can be viewed as a 10 x 10 grid, I would like to move all data up one row and then enter new data in the bottom row.
I know this can be done using a loop but this method would be too slow for my project which has a much larger array (990 x 1920).
I've tried ArrayBuffers and got nowhere.
The problem with the following test method (using subarray) is that although data1 size is specified as 100 it appears to reduce down to the subarray size when applied. I can then find no way to add further data at the end.
function initialize() {
data = new Uint32Array(100);
data1 = new Uint32Array(100);
for (var i = 0; i < data.length; i++) {
data[i] = i;
}
data1 = data.subarray(10);
console.log(data1);
}
Is there any way other than a loop to offset data in a typed array and add data at the end.
OK, I found a solution at Typed Arrays in Gecko 2: Float32Array concatenation and expansion using Set().
In JavaScript, a typed array is a fixed-length data structure which is based on ArrayBuffer i.e. a pre-allocated piece of memory anyway. Because of that, typed arrays do not have variable-length methods like push, pop etc.
So in order to offset a typed array you only have two options.
Preallocate a lot of memory in advance and shift the 'typed array', which is a actually a view over a memory block
var SIZE=100;
var SHIFT=10;
var buffer = new ArrayBuffer(100000); // preallocate a lot of memory
var data = new Uint32Array(buffer, 0, SIZE);
for (var i = 0; i < SIZE; i++) {
data[i] = i;
}
var data1 = new Uint32Array(buffer, Uint32Array.BYTES_PER_ELEMENT*SHIFT, SIZE)
data1[90]=100; //set the 101st (91st) element
console.log('data1', data1);//10,11,...98,99,100,0,0,0,0,0,0,0,0,0
Copy the slice of the old data into a new memory area.
var SIZE=100;
var SHIFT=10;
var data = new Uint32Array(SIZE);
for (var i = 0; i < SIZE; i++) {
data[i] = i;
}
var data1 = new Uint32Array(SIZE)
data1.set(data.subarray(SHIFT));
data1[90]=100; //set the 101st (91st) element
console.log('data1', data1); //10,11,...98,99,100,0,0,0,0,0,0,0,0,0
This is a classic space-time tradeoff.
The first option takes more memory but less cpu cycles, the second option is the other way round.
I have a very big array with car makes and models. I've already extracted the makes into a separate array, but I am struggling to extract the models while also maintaining their association to the make.
Here is a sample of the array:
var dataa = new Array
(
['Acura','','Integra','Mdx','Rl','Rsx','Slx','Tl','Tsx'],
['Aixam','','400','505','600'],
['Alfa romeo','','145','146','147','155','156'],
['Aston martin','','.','DBS','Db7','Db9']);
As you can see I have a multi-dimensional array with the car make (located at dataa[0][0]), then an empty value and then the model for this make.
I am using this code to to get the car makes:
This gives me the fist value of every nested array -> dataa[i][0]:
for (var i = 0; i < dataa.length; i++) {
document.write(dataa[i][0] + "<br>");
}
My problems start HERE.
I CAN NOT extract all models and assign them to the proper car make. I have tried for-loop's, loops with brakes, while loops and loops with conditional statements but I can't do it.
Please give me some advice here. Would jQuery or some other technology help me?
Put a loop inside your loop.
for (var i = 0; i < dataa.length; i++) {
document.write("<h2>Starting new inner loop!</h2><br>");
for (var j = 0; j < dataa[i].length; j++) {
document.write(dataa[i][j] + "<br>");
}
}
Now for every Array in the outer Array, you're doing a separate loop.
Here's a demo
How to add For loop information to Multi-Dimensional Array?
http://jsfiddle.net/MZj3L/
If I am trying this code get - map undefined. But how to save data something like to this ->
[[Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10], [Array[10]]
Thanks and sorry for my English language.
It seems in you want to initialize a multi dimensional array. Arrays are dynamic in JavaScript, you don't have to initialize them with a certain length. You could just do:
var map = [];
for(var a = 0; a < 10; a++){
map[a] = [];
}
This gives you an array containing 10 arrays.
Why are you getting undefined?
Because your syntax is way of. What map = [a][b]; does is creating an array with one element a and then accessing the bth element of that array and assign it to map.
So in the last iteration, it does:
map = [9][9];
which is the same as
tmp = [9];
map = tmp[9];
try something like
var map = [];
for(var a = 0; a < 10; a++){
map[a]=[];
for(var b = 0; b < 10; b++) {
map[a].push(b);
}
}
I am not sure what you want to do either but that's the only think I could do with your code ...