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

138
Views
How array will get assign to new key with in the same key with some more object?

I am trying to add existing key value to the new key wrt to the same key, I might not be able to explain it clearly with my words, but below data can make sense.

I have my data in below formate

const sampelData = [{
    'car1 critical':  [1, 1, 1, 1, 1, 1],
    'car1 good':  [1, 1, 1, 1, 1, 1, 1],
    'car1 warning':  [1, 1, 1, 1, 1, 1],
    'car2 critical':  [0, 0, 0, 0, 0,0],
    'car2 good':  [0, 0, 0, 0, 0, 0],
    'car2 warning': [0, 0, 0, 0, 0, 0]
 }]

and i want to manipulate in such a way that array will get assign to new key with in the same key with some more object

 Desired output :-
    [{
            'car1 critical':  {data:[1, 1, 1, 1, 1, 1], condition:"critical"},
            'car1 good':  {data:[1, 1, 1, 1, 1, 1], condition:"good"},
            'car1 warning':  {data:[1, 1, 1, 1, 1, 1], condition:"warning"},
            'car2 critical':  {data:[0, 0, 0, 0, 0, 0], condition:"critical"},
            'car2 good':  {data:[0, 0, 0, 0, 0, 0], condition:"good"},
            'car2 warning': {data:[0, 0, 0, 0, 0, 0], condition:"warning"}
   }]

If there are any other ways of handling such data, any info related to this will be very knowledgeable and helpful.

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

0

You can use Array map() with a for..in loop to itirate thru each object and modify its value. Here is an example:

const sampleData = [{
  'car1 critical': [1, 1, 1, 1, 1, 1],
  'car1 good': [1, 1, 1, 1, 1, 1, 1],
  'car1 warning': [1, 1, 1, 1, 1, 1],
  'car2 critical': [0, 0, 0, 0, 0, 0],
  'car2 good': [0, 0, 0, 0, 0, 0],
  'car2 warning': [0, 0, 0, 0, 0, 0]
}]

// map sampleData
sampleData.map(item => {
  // Iterate object in sampleData
  for (let i in item) {
    // assign new value to each item in object
    item[i] = {
      data: item[i],
      condition: i.split(' ')[1]
    }
  }
})

console.log(sampleData)

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!