简体   繁体   English

如何将新属性推入对象数组中的每个现有对象?

[英]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: 强调:

  1. Getting your qIndex should be done outside of a loop - as it stays the same no matter what. 获取qIndex应该在循环之外完成-因为无论如何它都保持不变。
  2. Using forEach loop instead of for loop is my personal preference for keeping code clean and readable - it would work exactly the same if you stick with for. 我个人倾向于使用forEach循环而不是for循环,以保持代码的清洁和可读性-如果坚持使用,它将完全一样。
  3. singleResult array seems unnecessary here as your goal was only to extend base objects in details with field single - so we completely removed it. 这里的singleResult数组似乎没有必要,因为您的目标只是使用字段single扩展详细的基础对象-因此我们将其完全删除。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM