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

230
Views
Convert a const Array to JSON with specific keys

I have an array like this:

const faces= [
    [[128516], "grinning face with smiling eyes", "20201001"],
    [[128512], "grinning face", "20201001"],
    [[128578], "slightly smiling face", "20201001"],
    [[128579], "upside-down face", "20201001"],
    [[128521], "winking face", "20201001"]
]

And I want to convert it to a formatted JSON like this using JavaScript:

[
  {
    "id": 128516,
    "name": "grinning face with smiling eyes",
    "date": "20201001"
  },
  {
    "id": 128512,
    "name": "grinning face",
    "date": "20201001"
  }
]

Any help is appreciated.

about 4 years ago · Santiago Gelvez
3 answers
Answer question

0

const faces = [
  [
    [128516], "grinning face with smiling eyes", "20201001"
  ],
  [
    [128512], "grinning face", "20201001"
  ],
  [
    [128578], "slightly smiling face", "20201001"
  ],
  [
    [128579], "upside-down face", "20201001"
  ],
  [
    [128521], "winking face", "20201001"
  ]
]

let result = []
faces.forEach(item => {
  let a = {
    id: item[0][0],
    name: item[1],
    date: item[2]
  }
  result.push(a)
})

console.log(JSON.stringify(result))

about 4 years ago · Santiago Gelvez Report

0

You could use reduce:

const faces = [
  [
    [128516], "grinning face with smiling eyes", "20201001"
  ],
  [
    [128512], "grinning face", "20201001"
  ],
  [
    [128578], "slightly smiling face", "20201001"
  ],
  [
    [128579], "upside-down face", "20201001"
  ],
  [
    [128521], "winking face", "20201001"
  ]
]

const result = faces.reduce((result, value) => {
  result.push({
    id: value[0][0],
    name: value[1],
    date: value[2]
  })
  return result
}, [])

console.log(result)

about 4 years ago · Santiago Gelvez Report

0

Simple one liner code using 'map' functionality of Arrays.

const faces= [
    [[128516], "grinning face with smiling eyes", "20201001"],
    [[128512], "grinning face", "20201001"],
    [[128578], "slightly smiling face", "20201001"],
    [[128579], "upside-down face", "20201001"],
    [[128521], "winking face", "20201001"]
];

let newObj = []; 

faces.map(eachObj => {
  const obj = {
   id: eachObj[0][0],
   name: eachObj[1],
   date: eachObj[2]
  };
  newObj.push(obj);
});

console.log('New Obj ==>', newObj);

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!