skip to Main Content

My cart data is something like this

{
    "cart" : [{
        "productId":"64d3a241c281d1adba26b454",
        "color":"pink",
        "count":1,
        "size":40

    },
    {
        "productId":"64d3a241c281d1adba26b454",
        "color":"blue",
        "count":3
//this object has no property named 'size'

    }]
}

My addToCart function is

const addToCart = catchAsync(async (req, res, next) => {
  const { cart } = req.body;
  const { _id } = req.user;
  console.log(cart)
  console.log(_id)
  let products = [];
  const user = await User.findById(_id);
 
  for (let i = 0; i < cart.length; i++) {
    let object = {};
    object.product = cart[i]._id;
    object.color = cart[i].color;
    object.size = cart[i].size 
    products.push(object);//products array should contain all objects(with and without size property)
  }
  let cartTotal = 0;
  for (let i = 0; i < products.length; i++) {
    cartTotal = cartTotal + products[i].price * products[i].count;
  }
  let newCart = await new Cart({
    products,
    cartTotal,
    user: user?._id,
  }).save();
  res.status(201).json({
    status: "success",
    data: {
      newCart,
    },
  });
});

How can i check if the property ‘size’ is present in every object of cart object array inside loop. I want all objects with size property and without size property to be added to products array

2

Answers


  1. You could use hasOwnProperty in JavaScript to achieve this. When looping the array of objects, use this to check "size" property exist or not.

    let propertyToCheck= 'size';
    
    for (let i = 0; i < cartCollection.length; i++) {
    
    if (cartCollection[i].hasOwnProperty(propertyToCheck)) {
        console.log(`${i} has the key "${propertyToCheck}"`);
    } else {
        console.log(`${i} does not have the key "${propertyToCheck}"`);
    }
    }
    

    Hope this helps!

    Login or Signup to reply.
  2. I guess this should do the job for you If I understood your problem .
    To check if the ‘size’ property is present in each object of the cart array and then add them to the products array accordingly, you can modify your loop like this:

    const addToCart = catchAsync(async (req, res, next) => {
      const { cart } = req.body;
      const { _id } = req.user;
      console.log(cart);
      console.log(_id);
      let products = [];
      const user = await User.findById(_id);
    
      for (let i = 0; i < cart.length; i++) {
        let object = {};
        object.product = cart[i]._id;
        object.color = cart[i].color;
        
        // Check if 'size' property is present in the current object
        if ('size' in cart[i]) {
          object.size = cart[i].size;
        }
    
        products.push(object);
      }
    
      let cartTotal = 0;
      for (let i = 0; i < products.length; i++) {
        cartTotal = cartTotal + products[i].price * products[i].count;
      }
    
      let newCart = await new Cart({
        products,
        cartTotal,
        user: user?._id,
      }).save();
      res.status(201).json({
        status: "success",
        data: {
          newCart,
        },
      });
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search