I am trying to figure out the best way to split a object that has many key value pairs over 800 points ( they are X and Y coordinates)
I would like to split X into its own array and Y into a separate array so that I can access X alone and Y alone.
Here is the structure of the points and what I have attempted
var path = {
0: [41, -73],
1: [41, -74],
2: [42, -75],
3: [43, -76]
};
for (var key in path) {
console.log("key " + key + " has value " + path[key]);
}
This will log
key 0 has value 41,-73
key 1 has value 42,-74
key 2 has value 43,-75
key 3 has value 44,-76
I need the first value to be stored in its own array and the second value into another array.
For example I would like to access all of X and Y as such,
var x = [41, 42, 43, 44]
var y = [-73,-74,-75,-76]
JS FIDDLE HERE http://jsfiddle.net/b3j4w9bv/14/
Thanks!
You could do something along these lines:
var path = {
0: [41, -73],
1: [41, -74],
2: [42, -75],
3: [43, -76]
};
var x_path = [];
var y_path = [];
for(var key in path)
{
//console.log("key " + key + " has value " + path[key]);
//create a split on , for x & y
var split_path = path[key];
//grab the value of the split:
var x = split_path[0];
var y = split_path[1];
//add the coords to their arrays.
x_path.push(x);
y_path.push(y);
//output result:
console.log(x_path);
console.log(y_path);
}
I haven't tested this in terms of perfomance..it might not be the most efficient
fiddle: http://jsfiddle.net/g123vyuv/
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.