【发布时间】:2022-01-24 16:23:41
【问题描述】:
原始数组
[{
"focusarea": "Leading Self",
"focusarea_color": "#589681",
"constructs": [
{
"construct": "construct 1",
"construct_color": "#ee9696",
"questionsArray": [
{
"question": "Motivates and empowers others to achieve their goals",
"raterAnswers": [
{
"raters": "Self",
"raters_color": "#d5a12c",
"answer": 2
},
{
"raters": "Manager",
"raters_color": "#c95c5c",
"answer": 1
},
{
"raters": "Direct Report",
"raters_color": "#1072c6",
"answer": 4
},
{
"raters": "Peers",
"raters_color": "#14d1b4",
"answer": 4
},
{
"raters": "Stackholder",
"raters_color": "#c471d8",
"answer": 4
},
{
"raters": "Co-workers",
"raters_color": "#69c3f4",
"answer": 2
}
]
},
{
"question": "Write your question here 10",
"raterAnswers": [
{
"raters": "Self",
"raters_color": "#d5a12c",
"answer": 2
},
{
"raters": "Manager",
"raters_color": "#c95c5c",
"answer": 3
},
{
"raters": "Direct Report",
"raters_color": "#1072c6",
"answer": 1
},
{
"raters": "Peers",
"raters_color": "#14d1b4",
"answer": 3
},
{
"raters": "Stackholder",
"raters_color": "#c471d8",
"answer": 1
},
{
"raters": "Co-workers",
"raters_color": "#69c3f4",
"answer": 4
}
]
}
]
}
]
}]
我想要的数组(这是控制台日志的结果)
[{
focusarea: 'Leading Self',
focusarea_color: '#589681',
construct: 'construct 1',
construct_color: '#ee9696',
question: 'Motivates and empowers others to achieve their goals',
answer: 3
}
{
focusarea: 'Leading Self',
focusarea_color: '#589681',
construct: 'construct 1',
construct_color: '#ee9696',
question: 'Write your question here 10',
answer: 3
}]
我得到的数组(这是第三个数组变量的结果)
{
"focusarea": "Leading Self",
"focusarea_color": "#589681",
"construct": "construct 1",
"construct_color": "#ee9696",
"question": "Write your question here 10",
"answer": 3
},
{
"focusarea": "Leading Self",
"focusarea_color": "#589681",
"construct": "construct 1",
"construct_color": "#ee9696",
"question": "Write your question here 10",
"answer": 3
},
我的代码
let lowestandHighestArray = []
for (let index = 0; index < detailedResultArray.length; index++) {
let focusAreaArray = {};
focusAreaArray.focusarea = detailedResultArray[index].focusarea // focus area name
focusAreaArray.focusarea_color = detailedResultArray[index].focusarea_color // focus area color
if(detailedResultArray[index].constructs) {
let constructs = detailedResultArray[index].constructs
for(let j = 0; j < constructs.length; j++) {
focusAreaArray.construct = constructs[j].construct // construct name
focusAreaArray.construct_color = constructs[j].construct_color // construct color
let questionsArray = constructs[j].questionsArray
for (let index2 = 0; index2 < constructs[j].questionsArray.length; index2++) {
focusAreaArray.question = questionsArray[index2].question
let answer = 0
for (let index3 = 0; index3 < questionsArray[index2].raterAnswers.length; index3++) {
answer = answer + questionsArray[index2].raterAnswers[index3].answer
}
focusAreaArray.answer = answer / questionsArray[index2].raterAnswers.length
console.log(focusAreaArray);
lowestandHighestArray.push(focusAreaArray)
}
}
}
}
如果我在控制台中打印结果,那么它可以按我的意愿工作,但是如果我将数组推入第三个变量,那么我会得到不同的结果。
在这里,当我将新元素推送到数组时,数组会覆盖其先前的元素,我不知道原因。谁能帮我解决这个问题?
【问题讨论】:
-
push不会覆盖任何内容。它只附加到一个数组。 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…
标签: javascript node.js arrays