[Vuejs]-Changing key value of object without discarding rest of keys

4👍

You’re returning result.name.replace(...) due to the implicit return of the ES6 arrow function – return result after modifying it. Destructuring is useful here, as is spreading:

.map(({ name, ...r }) => ({ ...r, name: name.replace(/ /g, "_")));

Alternate way:

.map(result => {
  result.name = result.name.replace(/ /g, "_");
  return result;
});

0👍

You need to return other properties as well in the map method, together with the modified name property.

const filteredResults = jsonResults
          .filter(result => result.name)
          .map(
            result => {
              return {
                ...result,
                name: result.name.replace(' ', '_')
              }
            }
          )
👤Boney

0👍

You are returning the just the name in the map funciton:

result => result.name.replace(' ', '_')

So rather do:

result => { result.name = result.name.replace(' ', '_'); return result; }
👤mathk

Leave a comment