उदाहरण
Object.assign() और map() का उपयोग करके कंडीशनल प्रॉपर्टी जोड़ें।
const details = {
john: {'studentName': 'John'},
david: {'studentName': 'David'},
mike: {'studentName': 'Mike'},
bob: {'studentName': 'Bob'},
carol: {'studentName': 'Carol'}
};
const join_values = ['David', 'Carol'];
const output = Object.assign(
{},
...Object.keys(details).map(key => ({
[key]: {
...details[key],
lastName: join_values.includes(details[key].studentName) ? 'Miller' : 'Smith'
}
}))
);
console.log(output);
प्रोग्राम चलाने के लिए:
node fileName.js
आउटपुट
PS C:\Users\Amit\JavaScript-code> node demo157.js
{ john: { studentName: 'John', lastName: 'Smith' }, david: { studentName: 'David', lastName: 'Miller' }, mike: { studentName: 'Mike', lastName: 'Smith' }, bob: { studentName: 'Bob', lastName: 'Smith' }, carol: { studentName: 'Carol', lastName: 'Miller' } }