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

105
Views
How to create new array base on JSON value

i have question about array, for example i have an array of object, like this

const data = [
  {
    level: 1,
    name: "Naruto",
  },
  {
    level: 2,
    name: "Dragon",
  },
  {
    level: 2,
    name: "Ball",
  },
  {
    level: 3,
    name: "Sasuke",
  },
] 

Now i want to create new array base on level , mean that after format it will look like this:

[
  [
    {
      name: "naruto"
    }
  ],
  [
    {
      name: "Ball"
    },
    { name: "Dragon" }
  ],
  [
    {name:"Sasuke"}
  ]
];

How can i do this, thanks you guys

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

0

I used a loop to handle your case

currentLevel - 1 is mapped with the index of the result's array

const data = [{
    level: 1,
    name: "Naruto",
  },
  {
    level: 2,
    name: "Dragon",
  },
  {
    level: 2,
    name: "Ball",
  },
  {
    level: 3,
    name: "Sasuke",
  },
];

let result = []
for (const currentValue of data) {

  const currentLevel = currentValue.level

  if (!result[currentLevel - 1]) {
    result[currentLevel - 1] = []
  }

  result[currentLevel - 1].push({
     name: currentValue.name
  })
}

//it's followed by index, the result will have some `undefined` if your levels have gaps. You can comment it out, if you want to keep `undefined` values
result = result.filter(item => item)

console.log({
  result
})

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!