首页 > 解决方案 > 如何在 Java Script/TestCafe 中获取多个键值对数组

问题描述

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 SOHAN SINGH PAUL ANDERSON

这适用于但出现 ESLINT 错误。 ESLint: iterators/generators require regenerator-runtime,这对于本指南来说太重了,无法允许它们。另外,应避免循环以支持数组迭代。(无限制语法)

请帮助我使用一些现代 JavaScript/TestCafe 代码实现上述目标。

标签: javascripttestingautomated-teststestcafekeyvaluepair

解决方案


const passnegerGroup = [
  { FIRSTNAME: 'RAHUL', LASTNAME: 'KUMAR' },
  { FIRSTNAME: 'RINA', LASTNAME: 'KUMAR' },
  { FIRSTNAME: 'SOHAN', LASTNAME: 'SINGH' },
  { FIRSTNAME: 'PAUL', LASTNAME: 'ANDERSON' },
];

const result = passnegerGroup.reduce((accum, cur) => `${accum} ${cur.FIRSTNAME} ${cur.LASTNAME} `, '');

console.log('result =', result);


推荐阅读