将具有嵌套对象的对象数组转换为简单的对象数组?
Convert an array of Objects with nested Objects into simple array of Objects?
我已经给出了一个嵌套对象数组,我想将它转换成一个简单的对象数组,但我不知道如何处理这个问题,谁能建议我解决这个问题的方法problem.i 已经尝试过,但我无法自己解决,或者我还没有找到类似的东西
const data = [
{
attachment: {
Name: {type: string, value: 'Amar'},
'Second Contact': {type: 'phoneNumber', value: '+91123587900'},
'First Contact': {type: 'phoneNumber', value: '+911234567890'},
'Registered Office Address': {
value: 'New Delhi',
type: 'string',
},
'Company Logo': {type: 'string', value: ''},
'Youtube ID': {type: 'string', value: ''},
},
creator: {
displayName: 'xyz',
phoneNumber: '+915453553554',
photoURL:
'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146',
},
},
{
attachment: {
Name: {type: string, value: 'hari'},
'Second Contact': {type: 'phoneNumber', value: '+91153587900'},
'First Contact': {type: 'phoneNumber', value: '+911264567890'},
'Registered Office Address': {
value: 'New Delhi mv',
type: 'string',
},
'Company Logo': {type: 'string', value: ''},
'Youtube ID': {type: 'string', value: ''},
},
creator: {
displayName: 'xyz',
phoneNumber: '+915453543554',
photoURL:
'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146',
},
},
];
预期的输出是这样的
[
{
Name:'Amar',
'Second Contact':+91123587900,
'First Contact':+911234567890,
'Registered Office Address':'New Delhi',
displayName:'xyz',
phoneNumber:'+915453553554'
},
{
Name:'hari',
'Second Contact':+91153587900,
'First Contact':+911264567890,
'Registered Office Address':'New Delhi mv',
displayName:'xyz',
phoneNumber:'+915453543554'
}
]
假设您需要获得一些属性。你可以简单地使用 map
。如果您需要一个适用于所有属性的解决方案,我可能会使用 object.keys
.
进行迭代
我在此处添加了两种解决方案
const data = [
{
"attachment": {
"Name": {"type": "string", "value": "Amar"},
"Second Contact": {"type": "phoneNumber", "value": "+91123587900"},
"First Contact": {"type": "phoneNumber", "value": "+911234567890"},
"Registered Office Address": {
"value": "New Delhi",
"type": "string"
},
"Company Logo": {"type": "string", "value": ""},
"Youtube ID": {"type": "string", "value": ""}
},
"creator": {
"displayName": "xyz",
"phoneNumber": "+915453553554",
"photoURL":
"https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146"
}
},
{
"attachment": {
"Name": {"type": "string", "value": "hari"},
"Second Contact": {"type": "phoneNumber", "value": "+91153587900"},
"First Contact": {"type": "phoneNumber", "value": "+911264567890"},
"Registered Office Address": {
"value": "New Delhi mv",
"type": "string"
},
"Company Logo": {"type": "string", "value": ""},
"Youtube ID": {"type": "string", "value": ""}
},
"creator": {
"displayName": "xyz",
"phoneNumber": "+915453543554",
"photoURL":
"https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146"
}
}
];
const result = data.map(item => {
return {
"Name": item.attachment.Name.value,
"Second Contact": item.attachment["Second Contact"].value
};
});
console.log(JSON.stringify(result));
// Another generic way
const result2 = data.map(item => {
const singleObj = {};
Object.keys(item.attachment).forEach(key => {
singleObj[key] = item.attachment[key].value;
});
return singleObj;
});
console.log(JSON.stringify(result2));
很确定,如果您对 javascript 有所了解,应该很容易解决。唯一棘手的部分可能是带有空格的字段,例如 "Second Contact".
解决这个特殊情况的方法如下:
const output = data.map(item => {
return {
name: item.attachment.Name.value,
'Second Contact': item.attachment['Second Contact'].value,
'First Contact': item.attachment['First Contact'].value,
'Registered Office Address':item.attachment['Registered Office Address'].value,
displayName: item.creator.displayName,
phoneNumber: item.creator.phoneNumber
};
})
console.log(output);
我已经给出了一个嵌套对象数组,我想将它转换成一个简单的对象数组,但我不知道如何处理这个问题,谁能建议我解决这个问题的方法problem.i 已经尝试过,但我无法自己解决,或者我还没有找到类似的东西
const data = [
{
attachment: {
Name: {type: string, value: 'Amar'},
'Second Contact': {type: 'phoneNumber', value: '+91123587900'},
'First Contact': {type: 'phoneNumber', value: '+911234567890'},
'Registered Office Address': {
value: 'New Delhi',
type: 'string',
},
'Company Logo': {type: 'string', value: ''},
'Youtube ID': {type: 'string', value: ''},
},
creator: {
displayName: 'xyz',
phoneNumber: '+915453553554',
photoURL:
'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146',
},
},
{
attachment: {
Name: {type: string, value: 'hari'},
'Second Contact': {type: 'phoneNumber', value: '+91153587900'},
'First Contact': {type: 'phoneNumber', value: '+911264567890'},
'Registered Office Address': {
value: 'New Delhi mv',
type: 'string',
},
'Company Logo': {type: 'string', value: ''},
'Youtube ID': {type: 'string', value: ''},
},
creator: {
displayName: 'xyz',
phoneNumber: '+915453543554',
photoURL:
'https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146',
},
},
];
预期的输出是这样的
[
{
Name:'Amar',
'Second Contact':+91123587900,
'First Contact':+911234567890,
'Registered Office Address':'New Delhi',
displayName:'xyz',
phoneNumber:'+915453553554'
},
{
Name:'hari',
'Second Contact':+91153587900,
'First Contact':+911264567890,
'Registered Office Address':'New Delhi mv',
displayName:'xyz',
phoneNumber:'+915453543554'
}
]
假设您需要获得一些属性。你可以简单地使用 map
。如果您需要一个适用于所有属性的解决方案,我可能会使用 object.keys
.
我在此处添加了两种解决方案
const data = [
{
"attachment": {
"Name": {"type": "string", "value": "Amar"},
"Second Contact": {"type": "phoneNumber", "value": "+91123587900"},
"First Contact": {"type": "phoneNumber", "value": "+911234567890"},
"Registered Office Address": {
"value": "New Delhi",
"type": "string"
},
"Company Logo": {"type": "string", "value": ""},
"Youtube ID": {"type": "string", "value": ""}
},
"creator": {
"displayName": "xyz",
"phoneNumber": "+915453553554",
"photoURL":
"https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146"
}
},
{
"attachment": {
"Name": {"type": "string", "value": "hari"},
"Second Contact": {"type": "phoneNumber", "value": "+91153587900"},
"First Contact": {"type": "phoneNumber", "value": "+911264567890"},
"Registered Office Address": {
"value": "New Delhi mv",
"type": "string"
},
"Company Logo": {"type": "string", "value": ""},
"Youtube ID": {"type": "string", "value": ""}
},
"creator": {
"displayName": "xyz",
"phoneNumber": "+915453543554",
"photoURL":
"https://www.google.com/url?sa=i&source=images&cd=&ved=2ahUKEwixw9uJrYjnAhWOe30KHa42AFwQjRx6BAgBEAQ&url=https%3A%2F%2Funsplash.com%2Fs%2Fphotos%2Fpic&psig=AOvVaw24C_LUOadGZAi3r2JtZe9b&ust=1579272070036146"
}
}
];
const result = data.map(item => {
return {
"Name": item.attachment.Name.value,
"Second Contact": item.attachment["Second Contact"].value
};
});
console.log(JSON.stringify(result));
// Another generic way
const result2 = data.map(item => {
const singleObj = {};
Object.keys(item.attachment).forEach(key => {
singleObj[key] = item.attachment[key].value;
});
return singleObj;
});
console.log(JSON.stringify(result2));
很确定,如果您对 javascript 有所了解,应该很容易解决。唯一棘手的部分可能是带有空格的字段,例如 "Second Contact".
解决这个特殊情况的方法如下:
const output = data.map(item => {
return {
name: item.attachment.Name.value,
'Second Contact': item.attachment['Second Contact'].value,
'First Contact': item.attachment['First Contact'].value,
'Registered Office Address':item.attachment['Registered Office Address'].value,
displayName: item.creator.displayName,
phoneNumber: item.creator.phoneNumber
};
})
console.log(output);