Thursday, 27 April 2017

Can I combine array destructuring and .trim()?

I am having an array of strings coming from a CSV file which I destructure in my node.js app.

Now I need the strings trimmed with .trim() but I wonder if there is an immediate way to do this. The below doesn't work:

// writing object with returned arrays retrieved from CSV
  playerRecordsArr.forEach((playerRecord) => {
    const [id.trim(), customFlag.trim(), countryCode.trim()] = playerRecord;
    resultObject[steamID] = {
      playerData: { customFlag, countryCode },
    };
  });

I guess the way to do it would be this, but I'd lose the destructuring goodness:

// writing object with returned arrays retrieved from CSV
  playerRecordsArr.forEach((playerRecord) => {
    const id = playerRecord[0].trim();
    const customFlag = playerRecord[1].trim();
    const countryCode = playerRecord[2].trim();
    resultObject[steamID] = {
      playerData: { customFlag, countryCode },
    };
  });



via Christian-Peter

No comments:

Post a Comment