This is similar to my real problem, but I feel that this is a problem that I need to solve without complicating the situation.
I need to create an array of objects, iterating through a 2d array. The 2d array looks something like “sampleArray” below.
let sampleArray = [
["open",2],
["high",3],
["low",5],
["close", 10],
["volume", 20],
["open",20],
["high",30],
["low",50],
["close", 100],
["volume", 21],
["open",21],
["high",33],
["low",51],
["close", 1],
["volume", 2],
["open",7],
["high",8],
["low",5],
["close", 11],
["volume", 22]
];
I am currently formatting an especially ugly API dataset and smoothing data to a 2-dimensional array. Now I need to put these values in an array of 5 objects, where each property of the object’s data is filled with all the values of this particular label.
newObject = [
{
data: [2,20,21, ...]
label: "open"
},
{
data: [3,50, ...]
label: "high"
},
{
data: [etc...]
label: "low"
},
{
data: [etc...]
label: "close"
},
{
data: [etc...]
label: "volume"
}
]
3 , , , , newArray [i] [ "label" ] undefined, , ( YET,
, ( , value) javascript.
function makeArray(array) {
let newArray = [];
for(let i = 0; i <= array.length; i++){
if(newArray[i]["label"] !== array[i][0]){
newArray.push({
"label": array[i][0],
"value": array[i][1]
})
}
else{
newArray.push({
"value": array[i][1]
})
}
}
return newArray;
}
let test = makeArray(sampleArray);
console.log(test);
, API , . , . , - . !