Currently, I have a nested array in JSON object. I would like to define the key and value for the following:
"phone": "", "email" : "", "fax": "", "website": ""
"city": "", "country": "", "house_number": "", "street name": ""
If one of these keys is missing, it needs to be filled as a default key and value as shown in the expected input.
Could anyone help how to do it in JavaScript?
Current Input:
{
"data": [
{
"name": "John",
"contact": {
"phone": "987-654-3210",
"email": "john123@xyz.com"
},
"address": {
"city": "Berlin",
"country": "Germany"
}
}
]
}
Expected Input:
{
"data": [
{
"name": "John",
"contact": {
"phone": "987-654-3210",
"email": "john123@xyz.com",
"fax": "",
"website": ""
},
"address": {
"city": "Berlin",
"country": "Germany",
"house_number": "",
"street name": ""
}
}
]
}
If you don't mind to mutate your original array, here's one solution:
const templates = {
contact: {
"phone": "",
"email": "",
"fax": "",
"website": ""
},
address: {
"city": "",
"country": "",
"house_number": "",
"street name": ""
}
}
source.data.forEach(entry => Object.entries(templates).forEach(([key, template]) => {
entry[key] = entry[key] || {};
Object.entries(template).forEach(([k, v]) => {
entry[key][k] = entry[key][k] || v;
});
}));
const source = {
"data": [{
"name": "John",
"contact": {
"phone": "987-654-3210",
"email": "john123@xyz.com"
},
"address": {
"city": "Berlin",
"country": "Germany"
}
}, {
"name": "Bob",
"contact": {
"phone": "123-456-7890",
"fax": "9876-5432",
"website": "bob.example.com"
},
"address": {
"city": "NYC",
"country": "USA",
"house_number": "701A"
}
}]
};
const templates = {
contact: {
"phone": "",
"email": "",
"fax": "",
"website": ""
},
address: {
"city": "",
"country": "",
"house_number": "",
"street name": ""
}
}
source.data.forEach(entry => Object.entries(templates).forEach(([key, template]) => {
entry[key] = entry[key] || {};
Object.entries(template).forEach(([k, v]) => {
entry[key][k] = entry[key][k] || v;
});
}));
console.log(source);