2

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!

4 Answers 4

2

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

Sign up to request clarification or add additional context in comments.

Comments

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)

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.

Comments

1

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)

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.