问题
I have this object which I have been trying to print in my ejs template without object syntaxes. I followed an answer here in StackOverflow and was successful in printing it without the syntaxes but it doesn't print everything in the nested object. What I mean is there are two nested objects in data(f and g). So the properties(keys) in those nested objects like name, email, phone, country, car are missing from the output. How can I also render that with their respected values?
Here's the object and the code which I used to format it:
router.get("/", async(req, res) => {
try{
let data = {
a: 'Tesla',
b: 'Mclaren',
c: 'Ferrari',
d: 'Lamborghini',
e: 'Lotus',
'f':{
name: 'John',
'e-mail': 'xyz@example.com',
phone: '+12345678',
country: 'USA',
car: 'Toyota Prius'
},
'g':{
name: 'Sophie',
'e-mail': 'xyz@example.com',
phone: '+12345678',
country: 'UK',
car: 'Nissan Bluebird'
},
h: 'Volkswagen',
i: 'Bugatti',
j:[
'% mileage',
'% top speed',
'% suspension',
'% navigation',
'% horsepower',
'% 0-60s'
]
}
var result = Object.entries(data).reduce((result, [key, value]) => {
key = key.replace(/([A-Z]|\d+)/g, ' $1').replace(/^(.)/, (unused, p1) => p1.toUpperCase());
if (!['string', 'number', 'boolean'].includes(typeof value)) {
value = Object.entries(value).map(([key, value]) => (typeof value == 'boolean') ? (value ? key : undefined) : value).filter(v => v !== undefined).join(',');
}
result.push(`${key}: ${value}`);
return result;
}, []);
var finalData = result.join('\n');
res.render("data", {data: finalData});
}catch(e){
req.flash("error", "Unable to retrieve data. Please try again!");
res.redirect("/");
}
});
This is the result if I print it in the console:
A: Tesla
B: Mclaren
C: Ferrari
D: Lamborghini
E: Lotus
F: John,xyz@example.com,+12345678,USA,Toyota Prius
G: Sophie,xyz@example.com,+12345678,UK,Nissan Bluebird
H: Volkswagen
I: Bugatti
J: % mileage,% top speed,% suspension,% navigation,% horsepower,% 0-60s
This is the result after I render it in my ejs template:
A: Tesla B: Mclaren C: Ferrari D: Lamborghini E: Lotus F: John,xyz@example.com,+12345678,USA,Toyota Prius G: Sophie,xyz@example.com,+12345678,UK,Nissan Bluebird H: Volkswagen I: Bugatti J: % mileage,% top speed,% suspension,% navigation,% horsepower,% 0-60s
Expected result:
A: Tesla
B: Mclaren
C: Ferrari
D: Lamborghini
E: Lotus
F:
Name: John
Email: xyz@example.com
Phone: +12345678
Country: USA
Car: Toyota Prius
G:
Name: Sophie
Email: xyz@example.com
Phone: +12345678
Country: UK
Car: Nissan Bluebird
H: Volkswagen
I: Bugatti
J:
% mileage
% top speed
% suspension
% navigation
% horsepower
% 0-60s
回答1:
This isn't pretty but should do the thing:
var result = Object.entries(data).reduce((result, [key, value]) => {
key = key.toUpperCase();
if (typeof value === 'object') {
result.push(`${key}:`);
if (Array.isArray(value)) {
value.forEach(item => result.push(item));
} else {
Object.entries(value).forEach(([key, value]) => {
key = key.charAt(0).toUpperCase() + key.slice(1)
result.push(`${key}: ${value}`);
})
}
} else {
result.push(`${key}: ${value}`);
}
return result;
}, []);
var finalResult = result.join("\n");
回答2:
Adding to the above answer use join
to get the proper expected result.
let data = {
a: 'Tesla',
b: 'Mclaren',
c: 'Ferrari',
d: 'Lamborghini',
e: 'Lotus',
'f':{
name: 'John',
'e-mail': 'xyz@example.com',
phone: '+12345678',
country: 'USA',
car: 'Toyota Prius'
},
'g':{
name: 'Sophie',
'e-mail': 'xyz@example.com',
phone: '+12345678',
country: 'UK',
car: 'Nissan Bluebird'
},
h: 'Volkswagen',
i: 'Bugatti',
j:[
'% mileage',
'% top speed',
'% suspension',
'% navigation',
'% horsepower',
'% 0-60s'
]
}
var result = Object.entries(data).reduce((result, [key, value]) => {
key = key.toUpperCase();
if (typeof value === 'object') {
result.push(`${key}:`);
if (Array.isArray(value)) {
value.forEach(item => result.push(item));
} else {
Object.entries(value).forEach(([key, value]) => {
key = key.charAt(0).toUpperCase() + key.slice(1)
result.push(`${key}: ${value}`);
})
}
} else {
result.push(`${key}: ${value}`);
}
console.log(result.join("\n"))
return result.join("\n");
}, []);
来源:https://stackoverflow.com/questions/59120590/nested-objects-key-is-missing-after-it-is-formatted-and-rendered