-1

I want to loop over each object within the object and recreate an array of object as shown below

First object

Object {
  "65657-33E-2442": Object {
    "description": "description qwerty",
    "imageUrl": "wwxxxxzdce.jpg",
    "time": "11/11/11 09:00",
    "title": "title",
  },
  "762873-773-33838": Object {
    "description": "2description qwerty2",
    "imageUrl": "2wwxxxxzdce.jpg2",
    "time": "22/22/22 09:00",
    "title": "2title2",
  },
}

I want to get a result like this :

[
      {
         "description": "description qwerty",
         "imageUrl": "wwxxxxzdce.jpg",
         "time": "11/11/11 09:00",
         "title": "title",
      },
      {
         "description": "2description qwerty2",
         "imageUrl": "2wwxxxxzdce.jpg2",
         "time": "22/22/22 09:00",
         "title": "2title2",
      },
]

#2 : this is the array that the object.values ​​function gives me

Array [
  ChildrenNode {
    "children_": SortedMap {
      "comparator_": [Function NAME_COMPARATOR],
      "root_": LLRBNode {
        "color": false,
.......
.......

My function

  const getAgenda = async () => {
    await getEvents().then((res) => {
      console.log(res) //this return first object
      console.log(Object.values(res)) //this return #2
    })
}

Thanks

2 Answers2

-1

Use Object.values:

Object.values({
    "65657-33E-2442": {
        "description": "description qwerty",
        "imageUrl": "wwxxxxzdce.jpg",
        "time": "11/11/11 09:00",
        "title": "title",
    },
    "762873-773-33838": {
        "description": "2description qwerty2",
        "imageUrl": "2wwxxxxzdce.jpg2",
        "time": "22/22/22 09:00",
        "title": "2title2",
    },
})

Will return:

[
  {
    description: 'description qwerty',
    imageUrl: 'wwxxxxzdce.jpg',
    time: '11/11/11 09:00',
    title: 'title'
  },
  {
    description: '2description qwerty2',
    imageUrl: '2wwxxxxzdce.jpg2',
    time: '22/22/22 09:00',
    title: '2title2'
  }
]
Benoît P
  • 3,179
  • 13
  • 31
-1

For a more complete example:

const input = {
  "65657-33E-2442": {
    "description": "description qwerty",
    "imageUrl": "wwxxxxzdce.jpg",
    "time": "11/11/11 09:00",
    "title": "title",
  },
  "762873-773-33838": {
    "description": "2description qwerty2",
    "imageUrl": "2wwxxxxzdce.jpg2",
    "time": "22/22/22 09:00",
    "title": "2title2",
  },
}

console.log(Object.values(input))

/*
This will print:
[
      {
         "description": "description qwerty",
         "imageUrl": "wwxxxxzdce.jpg",
         "time": "11/11/11 09:00",
         "title": "title",
      },
      {
         "description": "2description qwerty2",
         "imageUrl": "2wwxxxxzdce.jpg2",
         "time": "22/22/22 09:00",
         "title": "2title2",
      },
]
*/

IndevSmiles
  • 737
  • 6
  • 17