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

467 views Asked by At
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);
}

output :

RAHUL KUMAR RINA KUMAR SOHAN SINGH PAUL ANDERSON

This is working for but getting ESLINT error. ESLint: iterators/generators require regenerator-runtime, which is too heavyweight for this guide to allow them. Separately, loops should be avoided in favor of array iterations. (no-restricted-syntax)

Kindly help me achieve the above using some modern JavaScript/TestCafe codes.

2

There are 2 answers

4
farvilain On BEST ANSWER

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

0
Steve Tomlin On

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