Warm tip: This article is reproduced from serverfault.com, please click

javascript-如何在Java Script / TestCafe中获取多个键/值对数组

(javascript - How to fetch multiple key-value pairs array in Java Script/TestCafe)

发布于 2020-11-28 09:59:30
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代码帮助我实现以上目标。

Questioner
shashank shekhar
Viewed
0
farvilain 2020-11-28 18:14:16

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);