const passnegerGroup = [
{ FIRSTNAME: 'RAHUL', LASTNAME: 'KUMAR' },
{ FIRSTNAME: 'RINA', LASTNAME: 'KUMAR' },
{ FIRSTNAME: 'SOHAN', LASTNAME: 'SINGH' },
{ FIRSTNAME: 'PAUL', LASTNAME: 'ANDERSON' },
];
// I want to read each passenger's last name and first name and do some operations.
// Tried this code but this is
for (const key of Object.values(passnegerGroup)) {
console.log(key.FIRSTNAME, key.LASTNAME);
}
输出 :
拉尔·库玛(Rahul kumar)里纳·库玛(Rina kumar)索恩·辛格(Paul Paul Anderson)
这适用于但出现ESLINT错误。
ESLint: iterators/generators require regenerator-runtime
,对于本指南来说太重了,不允许他们使用。另外,应避免使用循环来进行数组迭代。(无限制语法)
请使用一些现代的JavaScript / TestCafe代码帮助我实现以上目标。
const passnegerGroup = [
{ FIRSTNAME: 'RAHUL', LASTNAME: 'KUMAR' },
{ FIRSTNAME: 'RINA', LASTNAME: 'KUMAR' },
{ FIRSTNAME: 'SOHAN', LASTNAME: 'SINGH' },
{ FIRSTNAME: 'PAUL', LASTNAME: 'ANDERSON' },
];
//No garantee that firstname comes before lastname
const result1 = passnegerGroup.map( u => Object.values(u) ).flat().join(' ');
console.log(result1);
const result2 = passnegerGroup.map( u => [u.FIRSTNAME, u.LASTNAME]).flat().join(' ');
console.log(result2);
感谢您的快速解决方案。它为我工作。虽然我不需要加入,所以直到passnegerGroup.map(u => Object.values(u))都可以使用并且可以正常工作。我会接受的。
它的工作原理,但它过度转换的杀伤力。最好更明确地显示输出-使用reduce方法而不是map,因为它返回一个需要进一步格式化的数组。
是的,您是对的,显然没有优化。@shashankshekhar,如果您使用的是20Mhz cpu,则在4大小的数组上使用这种3chained方法完全是过大了哈哈
它更多地是关于理解的步骤。Map方法返回一个数组,但是您需要一个字符串。纯编程原理。