Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
Iterate over an Object of objects and insert key value pair in each object

im trying to iterate over an Object which has lots of smaller objects inside it. I want to conditionally insert a key value pair in each of those smaller objects For Example :

const answers = {
    "question1": {
        answer: "Poor",
        metric: "focus" 
    },
    "question2": {
        answer: {agree: "yes", duration: "10"}
        metric: "exercise"
    }
}

so lets say the answer in question1 can either be poor, good or excellent with poor being equal to a score of 1, good a score of 5 and excellent a score of 10. Depending on the answer in question1 i would like to add a key value pair like score: 1 into the question1 object

Then for question2 we look at the duration and a duration of 10 gives us a score of 10 so we insert the key value pair like score: 10

So in the end i want to return an object that looks something like:

newAnswers = {
    "question1": {
        answer: "Poor",
        metric: "focus",
        score: 1
    },
    "question2": {
        answer: {agree: "yes", duration: "10"}
        metric: "exercise",
        score: 10
    }
}

In this example im showing just two questions for simplicity, but in reality it could be many, and the same logic will apply to all the other questions.

Thanks in advance

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Basically what you need is a looping logic

Somthing like this will help.

Logic

  • Loop through the entries in the object
  • Check the type of answer key in each object
  • If type is a string do mapping between "poor", "good", "excellent" options.
  • If its object check validity of duration and assign it to the score.

Working Fiddle

const answers = {
    "question1": {
        answer: "Poor",
        metric: "focus" 
    },
    "question2": {
        answer: {agree: "yes", duration: "10"},
        metric: "exercise"
    }
};

Object.entries(answers).forEach((item) => {
  const answer = item[1].answer;
  switch (typeof answer) {
    case "string":
      console.log(answer);
      const answerOptions = ["poor", "good", "excellent"];
      const scoreOptions = [1, 5, 10];
      const index = answerOptions.indexOf(answer.toLowerCase());
      if(index !== -1) {
        item[1].score = scoreOptions[index];
      }
      break;
    case "object":
      if (answer.duration && !isNaN(answer.duration)) {
        item[1].score = +answer.duration;
      }
      break;
  }
});
console.log(answers)

about 4 years ago · Juan Pablo Isaza Report

0

If I understand your question correctly, the question is how to insert a new attribute for each JSON object. If it is the case, you can do as follow.

answers["question1"]["score"] = 1;

Neglecting the conditional checking your logic (i.e. the conditional statement), the iteration could be:

$.each(answers, (q) => {
   q["score"] = x;
}

How to test it with Google Chrome Console

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!