Reduce object to array

Premise

We're working with this data:

const idToProductData = {
  2137: {
    price: 99.99,
    name: "Shampoo",
  },
  100: {
    price: 11.99,
    name: "Battery",
  },
  999: {
    price: 4.99,
    name: "Poster",
  },
};

And we want to just get the names of all the products:

const expectedResult = ["Shampoo", "Battery", "Poster"];

Solutions

// ✅
const allNames = Object.values(idToProductData).map((product) => product.name);
// ❌
// Wrong, because we're declaring array as mutable, the code is not imperative and we rely on side-effects
let allNames = [];
Object.values(idToProductData).forEach((product) => {
  allNames.push(product.name);
});
// ❌❌❌
// Wrong, for the same reasons as the code above.
// Also, this looks ugly af
let allNames = [];
for (const id in idToProductData) {
  allNames.push(idToProductData[id].name);
}

Resources

results matching ""

    No results matching ""