Alex
Alex

Reputation: 323

How to map an array of objects, return entire array with one altered

When I run the decreaseQuantity() function, I simply want to return the entire array of objects, but with the quantity of the one object altered (determined by passing the index, in this case "1", to the function).

Right now, I'm just returning the second Object (with the quantity correctly decreased by one), when I want to also have the first Object returned as well (unaltered). So I want to return the entire array of objects, but with the quantity altered for the specified object.

What am I doing incorrectly? Help appreciated.

let cars = 
    [
  {
  color: "red",
  type: "minivan",
  quantity: 7
},
  {
  color: "blue",
  type: "lambo",
  quantity: 5
}
]

    function decreaseQuantity(index) {
        if (cars[index]) {
          const updateCars = cars.map((product, i) => {
            if (index == i) return { ...product, quantity: product.quantity - 1};
          });
          return updateCars;
        }
    }

console.log(decreaseQuantity(1))

Upvotes: 4

Views: 397

Answers (4)

Marc
Marc

Reputation: 3876

let cars = [{
    color: "red",
    type: "minivan",
    quantity: 7
}, {
    color: "blue",
    type: "lambo",
    quantity: 5
}]

function decreaseQuantity(index) {

    if (cars[index]) {
        cars[index].quantity -= 1;
    }

    return cars;

}

console.log(decreaseQuantity(0));
console.log(decreaseQuantity(1));

Note: With that, you change the original object.

Upvotes: 0

Ulrich Stark
Ulrich Stark

Reputation: 411

You need to add return product for the else clause. Everything else is fine. You may try to run the code to my answer with the added else and see the console output for cars and updateCars.

let cars = [
  {
    color: "red",
    type: "minivan",
    quantity: 7
  },
  {
    color: "blue",
    type: "lambo",
    quantity: 5
  }
];

function decreaseQuantity(index) {
    if (cars[index]) {
      const updateCars = cars.map((product, i) => {
          if (index == i) {
            return { ...product, quantity: product.quantity - 1};
          } else {
            return product;
          }
      });
      return updateCars;
    }
}
const updateCars = decreaseQuantity(1);

console.log(cars);
console.log(updateCars);

Upvotes: 3

Farrukh Normuradov
Farrukh Normuradov

Reputation: 1184

Here is the playground.

const cars = [
  {
    color: "red",
    type: "minivan",
    quantity: 7
  },
  {
    color: "blue",
    type: "lambo",
    quantity: 5
  }
];

function decreaseQuantity(cars, index) {
  return cars.map((product, i) => {
    if (index === i) {
      return { ...product, quantity: product.quantity - 1 };
    } else {
      return product;
    }
  });
}

console.log(decreaseQuantity(cars, 0));


Upvotes: 0

mbojko
mbojko

Reputation: 14679

You almost got it right:

cars.map((product, i) => {
    if (index == i) return { ...product, quantity: product.quantity - 1};
});

simply add else return product. Although I would recommend the ternary operator:

cars.map((product, i) => index === i ? { ...product, quantity: product.quantity - 1} : product;

Upvotes: 1

Related Questions