-1

I have the code like this one below, woluld like to convert to another format

 result :  [
  { _id: 'Completed', count: 1 },
  { _id: 'Assigned', count: 9 },
  { _id: 'Closed', count: 2 },
  { _id: 'In-Progress', count: 5 }
]

I want the format as below

 result :  [
  { Completed :1 },
  { Assigned, : 9 },
  { Closed,: 2 },
  { In-Progress : 5 }
]

Thanks

  • 1
    Hey Syed, if you want us to help, could you give some more background info, please? Like what language this is? What data structures you are using, etc? Otherwise, there is a high chance this question was already answered, so look for that too. – Yamm Elnekave Mar 13 '23 at 05:57
  • sorry, in javascript, i have edited my question – Syed Noohu Mar 13 '23 at 06:01
  • https://stackoverflow.com/questions/17367889/what-is-the-concept-of-array-map – derpirscher Mar 13 '23 at 06:05
  • Sorry, my idea was like result = { Completed :1 , Assigned, : 9 , Closed: 2, In-Progress : 5 } – Syed Noohu Mar 13 '23 at 07:25

1 Answers1

1

Try using map()

const result =  [
  { _id: 'Completed', count: 1 },
  { _id: 'Assigned', count: 9 },
  { _id: 'Closed', count: 2 },
  { _id: 'In-Progress', count: 5 }
]

const formattedArray = result.map((item, index) => {
   return { [item._id]: item.count }
})

console.log(formattedArray)
Srushti Shah
  • 810
  • 3
  • 17
  • Thanks for your solution, Its works fine, i was trying to use 'for of' for (const object of result) { arr.push({ [object._id]: object.count} ); } – Syed Noohu Mar 13 '23 at 07:02
  • I found thesolution var result = [ { _id: 'Completed', count: 21 }, { _id: 'Assigned', count: 91 }, { _id: 'Closed', count: 22 }, { _id: 'In-Progress', count: 25 } ] let obj ={} for (const object of result) { obj[object._id] = object.count } Thanks – Syed Noohu Mar 13 '23 at 07:36