为此,请使用Object.assign()。以下是代码-
const details = {john:{'studentName':'John'},david:{'studentName':'David'},mike:{'studen tName':'Mike'},bob:{'studentName':'Bob'},carol:{'studentName':'Carol'}}, join_values = ['David', 'Carol'], 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.
输出结果
在这里,我的文件名为demo157.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' } }