React - convert object into array of objects with properties

后端 未结 4 1050
时光说笑
时光说笑 2021-01-25 07:15

I have the following object

\"data\":{  
         \"name 1\":\"a\",
          \"name 2\":\"b\",
          \"name 3\":\"b\",
        },

How can

4条回答
  •  独厮守ぢ
    2021-01-25 08:00

    If you use a reduce function you can do the following to achieve your goal

    Object.keys(data).reduce((array, key) => {
        return [...array, {key: data[key]}]
    }, [])
    

    Reduce is a cool function that iterates and combine data into one single item (could be 1 object, array, integer, etc).

    Object.keys() is a way to get each key from the current object and be able to iterate over each.

提交回复
热议问题