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

100
Views
Placing an array in a key-value pair in the the high-level object with multiple values

I am trying to move everything in the Array Results outside and into the original object

this is the object and the array Results can have multiple values

{
  "Name": "John",
  "Results": [
    {
      "Type": "DB",
      "Immediate_Action": "No",
    }, 
{
      "Type": "system",
      "Immediate_Action": "Yes",
    }
  ]
}

It should look like this

{
  "Name": "John",
  "Type": ["DB","system"]
  "Immediate_Action": ["No","Yes"]
}

What I have so far is this data will be populated not at once but will be looped until the entire object is completed. mapped[key] is probably wrong.

const mapOscarResults = ({ data }) => {
    return data.map(entry => {
        let mapped = {...entry};
        entry.Results.forEach(key => {
            let Type =  mapped[key.Type]

            if (mapped[key]) {
                mapped[key].push(entry.Results[key]);
            } else {
                mapped[key] = [entry.Results[key]];
            }
        });
        return mapped;
    });
};
about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

It's a good case for reduce. Just collect the values for Results keys in arrays...

const input = {
  "Name": "John",
  "Results": [{
      "Type": "DB",
      "Immediate_Action": "No",
    },
    {
      "Type": "system",
      "Immediate_Action": "Yes",
    }
  ]
}

let output = { Name: input.Name }
output.Results = input.Results.reduce((acc, el) => {
  let keys = Object.keys(el);
  keys.forEach(key => {
    if (!acc[key]) acc[key] = [];
    acc[key].push(el[key]);
  });
  return acc;
}, {});

console.log(output)

about 4 years ago · Santiago Gelvez Report

0

This should do the trick

const data = {
  "Name": "John",
  "Results": [
    {
      "Type": "DB",
      "Immediate_Action": "No",
    }, 
    {
      "Type": "system",
      "Immediate_Action": "Yes",
    }
  ]
};

const transformData = data => {
  const newData = { Name: data.Name };
  newData.Type = [];
  newData.Immediate_Action = [];

  data.Results.forEach(({ Type, Immediate_Action }) => {
    newData.Type.push(Type);
    newData.Immediate_Action.push(Immediate_Action);
  });

  return newData;
}

const newData = transformData(data);
console.log(newData);
about 4 years ago · Santiago Gelvez Report

0

Try using reduce and entries

const data = {
  "Name": "John",
  "Results": [{
      "Type": "DB",
      "Immediate_Action": "No",
    },
    {
      "Type": "system",
      "Immediate_Action": "Yes",
    }
  ]
};

const restructure = obj => {
  obj.Results = obj.Results.reduce((accumu, current) => {
    for (const [key, val] of Object.entries(current)) {
      accumu[key] = [...accumu[key] ?? '', val] ;
    }
    return accumu;
  }, {});
  return obj;
}

console.log(restructure(data));

about 4 years ago · Santiago Gelvez 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!