假设以下是我们的对象-
const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ]
您可以根据空间使用split()。
以下是代码-
const employeeDetails = [ { employeeName: "John Smith", employeeTechnology: "JavaScript HTML" }, { employeeName: "David Miller", employeeTechnology: "Java Angular" } ] const objectValues = employeeDetails.map(emp => { var [technology1, technology2] = emp.employeeTechnology.split(/\s/); return { technology1, technology2, employeeName: emp.employeeName } }); console.log(objectValues);
要运行上述程序,请使用以下命令-
node fileName.js.
在这里,我的文件名为demo244.js。
输出结果
以下是控制台上的输出-
PS C:\Users\Amit\javascript-code> node demo244.js [ { technology1: 'JavaScript', technology2: 'HTML', employeeName: 'John Smith' }, { technology1: 'Java', technology2: 'Angular', employeeName: 'David Miller' } ]