这类似于我的实际问题,但我觉得说明我需要解决的问题,而不会使事情复杂化。
我需要通过迭代2d数组来创建一个对象数组。 2d数组看起来像" sampleArray"下面。
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]
];
我目前正在格式化一组特别丑陋的API数据,并将数据压缩为二维数组。我现在需要的是将这些值放入一个包含5个对象的数组中,其中每个对象的数据属性都由该特定标签的所有值填充。
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个小时,并且觉得我发现这一切都错了,我不断收到新错误[i] ["标签" ]是未定义的,我明白它为什么会发生(没有该属性的对象存在YET, 我只是不确定如何表达这个想法(如果具有该标签的对象尚未存在,请在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做好准备,觉得我现在只是跑到位。我更倾向于在正确的方向上找到一个点,而不是直接的答案。我打算掏腰包,看看有人能在早上给我一点透视。谢谢!
答案 0 :(得分:5)
您可以使用reduce
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]
];
let newObject = Object.values(sampleArray.reduce((c, [n, v]) => {
c[n] = c[n] || {label: n,data: []};
c[n].data.push(v);
return c;
}, {}));
console.log(newObject);
答案 1 :(得分:2)
使用Vanilla JS:
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]
];
function transform(sampArr) {
let obj = {}; // A map equivalent to store repeated value
// Loop through each array within array
sampArr.forEach(function(arr) {
if (!obj[arr[0]]) {
obj[arr[0]] = {}; // Instantiate the map the first time
}
if (!obj[arr[0]].data) {
obj[arr[0]]["data"] = []; // First time instantiate the array
}
obj[arr[0]].data.push(arr[1]); // Push the repeated values
});
// Create the structure you want
return Object.keys(obj).map(function(key) {
return {
data: obj[key].data,
label: key
};
});
}
console.log(transform(sampleArray));
&#13;
答案 2 :(得分:1)
您可以在转到最终值之前引入一个临时变量 -
const tempObject = sampleArray.reduce((acc, arr) => {
if (acc.hasOwnProperty(arr[0]) {
return acc[arr[0]].push(arr[1]);
}
return { ...acc, { [arr[0]]: [arr[1]] };
}), {});
现在tempObject将是 -
{
'open': [2, 20, 21, ...],
'high': [3, 50, ...],
'low': [etc, ...],
'close': [etc, ...],
'volume': [etc, ...]
}
然后你可以将上面的对象作为,
const finalData = Object.keys(tempObject).map(key => (
{
data: tempObject[key],
label: key
}
);
现在 finalData 将成为所需的对象。