[英]Create an array of objects from a nested object
I've a nested object 'empData'. 我有一个嵌套的对象'empData'。 I need to iterate over each of the keys in my object and return an array of objects. 我需要遍历对象中的每个键并返回对象数组。 Each key inside my object 'empData' is again an object. 我的对象'empData'中的每个键还是一个对象。
Now, I need to return an array of object as: 现在,我需要返回一个对象数组:
Expected output: 预期产量:
[{
"team": "AUS",
"name": "John"
}, {
"team": "CAN",
"name": "Steve"
}, {
"team": "IND",
"name": "Robbie"
}, {
"team": "IRE",
"name": "James"
}, {
"team": "USA",
"name": "Austin"
}];
My Code: 我的代码:
function getData() { const empData = { "AUS": { "isRetired": true, "name": "John" }, "CAN": { "name": "Steve" }, "IND": { "name": "Robbie" }, "IRE": { "name": "James" }, "USA": { "name": "Austin" } }; Object.keys(empData).map(function(eachKey) { const obj = { team: eachKey, name: eachKey.name }; console.log(obj); return obj; }); }
<button onclick="getData()">Get Data</button>
Could someone please help me with this? 有人可以帮我吗?
change name: eachKey.name
更改name: eachKey.name
name: empData[eachKey].name
function getData() {
const empData = {
"AUS": {
"isRetired": true,
"name": "John"
},
"CAN": {
"name": "Steve"
},
"IND": {
"name": "Robbie"
},
"IRE": {
"name": "James"
},
"USA": {
"name": "Austin"
}
};
Object.keys(empData).map(function(eachKey) {
const obj = {
team: eachKey,
name: empData[eachKey].name
};
console.log(obj);
return obj;
});
}
You can use Object.entries
to convert the object into an array. 您可以使用Object.entries
将对象转换为数组。 Use map
to loop thru the array. 使用map
遍历数组。
Object.entries
first element is the key and the second is the value. Object.entries
第一个元素是键,第二个是值。
const empData = { "AUS": { "isRetired": true, "name": "John" }, "CAN": { "name": "Steve" }, "IND": { "name": "Robbie" }, "IRE": { "name": "James" }, "USA": { "name": "Austin" } }; var result = Object.entries(empData).map(([team, {name}]) => ({team,name})); console.log(result);
.map
the Object.entries
instead, so you can extract both the key (to get the team
and the value (so you can get the name
) at once: .map
Object.entries
代替,因此您可以一次提取键(以获取team
和值)(从而可以获取name
):
const empData = { "AUS": { "isRetired": true, "name": "John" }, "CAN": { "name": "Steve" }, "IND": { "name": "Robbie" }, "IRE": { "name": "James" }, "USA": { "name": "Austin" } }; const getData = () => { const result = Object.entries(empData).map(([team, { name }]) => ({ team, name })); console.log(result); };
<button onclick="getData()">Get Data</button>
You can use for..in
to iterate iver the object and create a new object with relevant keys and values and push to another array 您可以使用for..in
迭代对象,并使用相关的键和值创建一个新对象,然后推入另一个数组
function getData() { const empData = { "AUS": { "isRetired": true, "name": "John" }, "CAN": { "name": "Steve" }, "IND": { "name": "Robbie" }, "IRE": { "name": "James" }, "USA": { "name": "Austin" } }; let newData = []; for (let keys in empData) { newData.push({ team: keys, name: empData[keys].name }) } console.log(newData) } getData()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.