I'm trying to add elements to a JSON from items of an array, but I'm struggling to pass the elements to the JSON.
here is my code:
var j = {
"Root": {
"a": "1800,1200,3100",
"b": "1500,1999,2001",
"c": "40,60,50",
"d": "this is not needed",
"e": "nor this one"
}
};
var root = j.Root,
l = root.a.split(",").length,
hash = ["a", "b", "c"];
for (var i = 0; i < l; i++) {
for (var x = 0; x < hash.length; x++) {
root['row_' + i] = {
"a": root.a.split(",")[i],
"b": root.b.split(",")[i],
"c": root.c.split(",")[i] // I don't want to do this for each key
};
}
}
for (var x = 0; x < hash.length; x++) {
delete root[hash[x]];
}
console.log(JSON.stringify(j));
My code is working, but I'm looking for a proper way to use the elements of my array because I will have more than a,b,c
PS: not all key will be used