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

137
Views
Transform an array of objects to key value pairs with one of the values as key

I have an array of objects like this:

  const arr = [
{
  question_Number: 205,
  question_Text: "Enter Engine Number",
},
{
  question_Number: 497,
  question_Text: "Whether imported?",
},
{
  question_Number: 547,
  question_Text: "Whether goods are new?",
},
{
  question_Number: 206,
  question_Text: "Select Vehicle Condition",
},

];

and I want to transform it into an object of key/value pairs as this:

  const result = {
205: {
  type: "string",
  title: "Enter Engine Number",
},
497: {
  type: "string",
  title: "Whether imported?",
},
547: {
  type: "string",
  title: "Whether imported goods are new?",
},
206: {
  type: "string",
  title: "Select Vehicle Condition",
},

};

How can I achieve this result using plain javascript or even lodash?

Thanks.

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

0

You could map entries with new objects.

const
    data = [{ question_Number: 205, question_Text: "Enter Engine Number" }, { question_Number: 497, question_Text: "Whether imported?" }, { question_Number: 547, question_Text: "Whether goods are new?" }, { question_Number: 206, question_Text: "Select Vehicle Condition" }],
    result = Object.fromEntries(data.map(({ 
        question_Number,
        question_Text: title
    }) => [question_Number, { type: 'string', title }]));
    
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Report

0

You can do this with built in things but I think this would be more understandable to see what is going on, something real quick:

const input = [
    {
        question_Number: 205,
        question_Text: "Enter Engine Number",
    },
    {
        question_Number: 497,
        question_Text: "Whether imported?",
    },
    {
        question_Number: 547,
        question_Text: "Whether goods are new?",
    },
    {
        question_Number: 206,
        question_Text: "Select Vehicle Condition",
    }
]
const transformArray = (arr) => {
    let result = {}
    for (let i = 0; i < arr.length; i++) {
        result[arr[i].question_Number] = { type: typeof arr[i].question_Text, title: arr[i].question_Text }
    }
    return result
}

console.log(transformArray(input))

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!