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

83
Views
moving a key value pair out of an array

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

this is the object

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

It should look like this

{
  "Name": "John",
  "Type": "DB",
  "Immediate_Action": "No",
}

What I have so far is this

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

You can simply spread the Results array into an Object.assign() call.

const input = { "Name": "John", "Results": [{ "Type": "DB", "Immediate_Action": "No", }, { "Another": "value" }] };

const { Results, ...refactored } = input;
Object.assign(refactored, ...Results);

console.log(refactored)

about 4 years ago · Santiago Gelvez Report

0

however, it will join first Result with index 0, you can expand it

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

const mapOscarResults = (data) => {
for (let i in Object.keys(data)){
    if (Array.isArray(data[Object.keys(data)[i]])){
        newKey = data[Object.keys(data)[i]][0]
        data = {... data, ...newKey}
        delete data[Object.keys(data)[i]]
    }
}
return data
};

console.log(mapOscarResults(data))

about 4 years ago · Santiago Gelvez Report

0

This code works for your example:

const { Results: results, ...rest } = {
  "Name": "John",
  "Results": [
    {
      "Type": "DB",
      "Immediate_Action": "No",
    }
  ]
}

const res = {...rest, ...results.reduce((prev, curr) => ({
  ...prev,
  ...curr
}), {})}

console.log(res)

But I don't know what you expect when the Results array has more than one element.

In that condition, if this code does not fill your needs, ask me to change it.

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!