[英]How to push new property into each existing object within object array?
I'm trying to retrieve a single result from a multi-dimensional array and then push that result into each object contained within an object array. 我正在尝试从多维数组中检索单个结果,然后将其推入对象数组中包含的每个对象中。
Here is my code; 这是我的代码;
var data = { "questions": ["Q1", "Q2", "Q3"], "details": [{ "name": "Alex", "values": [27, 2, 14] }, { "name": "Bill", "values": [40, 94, 18] }, { "name": "Gary", "values": [64, 32, 45] }] } var question = "Q1"; var singleResult = []; for (var i = 0; i < data.details.length; i++) { var qIndex = data.questions.indexOf(question) singleResult.push(data.details[i].values[qIndex]) } for (var i = 0; i < singleResult.length; i++) { data.details.push({ single: singleResult[i] }) } console.log(data.details)
As you can see it is pushing a new object into the array where as instead I would like the single result to be pushed into each of the existing 3 objects. 如您所见,它正在将一个新对象推入数组,而我希望将单个结果推入现有的3个对象。
So my new array should look like; 所以我的新数组应该看起来像;
[{
"name": "Alex",
"values": [27, 2, 14],
"single": 27
}, {
"name": "Bill",
"values": [40, 94, 18],
"single": 40
}, {
"name": "Gary",
"values": [64, 32, 45],
"single": 64
}]
I thought running a loop with .concat
would do the trick, but sadly it wasn't the case (for me at least!). 我以为使用
.concat
运行循环可以.concat
,但遗憾的是事实并非如此(至少对我而言!)。
Hope everything is clear, thanks in advance for any help/advance! 希望一切顺利,在此先感谢您的帮助/提前!
I would refactor it like this: 我会像这样重构它:
var data = { "questions": ["Q1", "Q2", "Q3"], "details": [{ "name": "Alex", "values": [27, 2, 14] }, { "name": "Bill", "values": [40, 94, 18] }, { "name": "Gary", "values": [64, 32, 45] }] } var question = "Q1"; var qIndex = data.questions.indexOf(question) data.details.forEach((obj) => { obj.single = obj.values[qIndex]; }); console.log(data.details)
Highlights: 强调:
You can use indexOf()
to get index of question and then map()
to get modified array. 您可以使用
indexOf()
获取问题的索引,然后使用map()
获取修改后的数组。
var data = {"questions":["Q1","Q2","Q3"],"details":[{"name":"Alex","values":[27,2,14]},{"name":"Bill","values":[40,94,18]},{"name":"Gary","values":[64,32,45]}]} var question = "Q1"; var qIndex = data.questions.indexOf(question); var result = data.details.map(function(e) { var o = JSON.parse(JSON.stringify(e)); o.single = e.values[qIndex]; return o; }); console.log(result);
var data = { "questions": ["Q1", "Q2", "Q3"], "details": [{ "name": "Alex", "values": [27, 2, 14] }, { "name": "Bill", "values": [40, 94, 18] }, { "name": "Gary", "values": [64, 32, 45] }] } var question = "Q1"; var singleResult = []; for (var i = 0; i < data.details.length; i++) { var qIndex = data.questions.indexOf(question) singleResult.push(data.details[i].values[qIndex]) } for (var i = 0; i < singleResult.length; i++) { data.details[i].single = singleResult[i]; } console.log(data.details)
var data = {"questions": ["Q1", "Q2", "Q3"],"details": [{"name": "Alex","values": [27, 2, 14]}, {"name": "Bill","values": [40, 94, 18]}, {"name": "Gary","values": [64, 32, 45]}]}, question = "Q1", qIndex = data.questions.indexOf(question); for (var i = 0, len = data.details.length; i < len; i++) { data.details[i].single = data.details[i].values[qIndex]; } console.log(data.details);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.