JavaScript How to push new property into each existing object within object array? push property into object javascript,push property into object,push

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)
Answer:1

You can use indexOf() to get index of question and then map() to get modified array.

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);
Answer:2

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)
Answer:3

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)
Answer:4

I created a chart using the dimple.js library with the following code: var myChart = new dimple.chart(svg, data); var x = myChart.addMeasureAxis("x", "x-measure"); x.tickFormat = "%"; var y = myChart....

I created a chart using the dimple.js library with the following code: var myChart = new dimple.chart(svg, data); var x = myChart.addMeasureAxis("x", "x-measure"); x.tickFormat = "%"; var y = myChart....

I have a JavaScript associative array, say: var myArray = new Object(); myArray["id1"] = "completed"; myArray["id2"] = "notCompleted"; myArray["id3"] = "started"; How can I update value of each item ...

I have a JavaScript associative array, say: var myArray = new Object(); myArray["id1"] = "completed"; myArray["id2"] = "notCompleted"; myArray["id3"] = "started"; How can I update value of each item ...

  1. update each value in dictionary python
  2. php update each value in array
  3. c# update each value in dictionary
  4. python update each value
  5. sql update different value each row
  6. loadrunner update value on each occurrence
  7. c# update value of each item in list

How can i stop an request which is already being processing in backend ? I am using EXT Js Framework,There are two button , 1.Search button This button queries the back end and fetches the ...

How can i stop an request which is already being processing in backend ? I am using EXT Js Framework,There are two button , 1.Search button This button queries the back end and fetches the ...

I want to calculate the height from the top to the point where i have added link. How can i calculate height for example we use: w = $(window).height(); to calculate the height of window. ...

I want to calculate the height from the top to the point where i have added link. How can i calculate height for example we use: w = $(window).height(); to calculate the height of window. ...

  1. calculate height using weight and bmi
  2. calculate height using distance and angle
  3. calculate height using shadow
  4. calculate height using camera
  5. calculate height using ulna length
  6. calculate height using knee height
  7. calculate height using time
  8. calculate height using css
  9. calculate height using tangent
  10. calculate height using degrees
  11. calculate height using trigonometry
  12. calculate height using bmi
  13. calculate height using pressure
  14. calculate height using weight
  15. calculate height using ulna
  16. calculate height using angle
  17. calculate height using jquery
  18. calculate using height
  19. calculate height building using trigonometry
  20. calculate height mountain using trigonometry