React - convert object into array of objects with properties

后端 未结 4 1043
时光说笑
时光说笑 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 07:41

    The solution provided by Tall Paul will work perfectly but you can also use Object.entries(). It states that

    The Object.entries() method returns an array of a given object's own enumerable property [key, value] pairs, in the same order as that provided by a for...in loop (the difference being that a for-in loop enumerates properties in the prototype chain as well).

    so you can try something like this

    let result = Object.entries(data);
    result.map((item, index)=>{
          console.log('key is:- ', item[0], ' and value is:- ', item[1]); 
    });
    

提交回复
热议问题