React - convert object into array of objects with properties

后端 未结 4 1048
时光说笑
时光说笑 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:54

    you can get all the keys in the object in an array using Object.keys function and use map function to get the desired output.

    This will convert to array of objects that will keep both the name and data.

    var data = {  
              "name 1":"a",
              "name 2":"b",
              "name 3":"b",
    }
    var res = Object.keys(data).map(function(name){
    	var obj = {};
    	obj[name] = data[name];
    	return obj;
    });
    
    console.log(res);

提交回复
热议问题