ggt
ggt

Reputation: 331

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.

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!).

Hope everything is clear, thanks in advance for any help/advance!

Upvotes: 2

Views: 1158

Answers (4)

Yosvel Quintero
Yosvel Quintero

Reputation: 19070

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);

Upvotes: 1

Nenad Vracar
Nenad Vracar

Reputation: 122027

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);

Upvotes: 2

Ernest Nowacki
Ernest Nowacki

Reputation: 241

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.
  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.
  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.

Upvotes: 2

Prasad Rasapally
Prasad Rasapally

Reputation: 21

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)

Upvotes: 1

Related Questions