skip to Main Content

I am trying to create cart using js

  function fuBuyNow() {

        var prodqty = 1;
        var prodid = localStorage.ls_productID;
        var prodprice = $('#ppriceoffer').html();
        var cartItem = {
            productid: prodid,
            productqty: prodqty,
            productprice: prodprice
        };
        var cartItemJSON = JSON.stringify(cartItem);
        console.log(cartItemJSON);

        var cartArray = new Array();
        // If javascript shopping cart session is not empty
        if (localStorage.getItem('shopping-cart')) {
            cartArray = JSON.parse(localStorage.getItem('shopping-cart'));
        }
        cartArray.push(cartItemJSON);

        var cartJSON = JSON.stringify(cartArray);
        localStorage.setItem('shopping-cart', cartJSON);

    }

now i have cart data in an array, I want to sum of productid = 10005, productid = 10001 price and create new array

var obj = "[{"slno":"1","productid":"10001","qty":"1","price":"250"},{"slno":"2","productid":"10005","qty":"1","price":"350"},{"slno":"3","productid":"10001","qty":"1","price":"250"},{"slno":"4","productid":"10003","qty":"1","price":"450"},{"slno":"5","productid":"10005","qty":"1","price":"350"}]";

2

Answers


  1. You can achieve this by using JavaScript’s array methods along with jQuery if you’re working with DOM elements. Here’s how you can do it:

    var obj = [{"slno":"1","productid":"10001","qty":"1","price":"250"},{"slno":"2","productid":"10005","qty":"1","price":"350"},{"slno":"3","productid":"10001","qty":"1","price":"250"},{"slno":"4","productid":"10003","qty":"1","price":"450"},{"slno":"5","productid":"10005","qty":"1","price":"350"}];
    
    // Filter the array to get only the items with productid = 10005 or productid = 10001
    var filteredArray = obj.filter(function(item) {
        return item.productid === "10005" || item.productid === "10001";
    });
    
    // Calculate the total price
    var totalPrice = filteredArray.reduce(function(acc, item) {
        return acc + parseInt(item.price);
    }, 0);
    
    // Create a new array with the total price
    var newArray = [{ "totalPrice": totalPrice }];
    
    console.log(newArray);

    This code filters the original array to include only items with productid equal to "10005" or "10001", then calculates the total price of those items, and finally creates a new array with the total price. You can use this newArray to display or manipulate the total price as needed. If you have any further questions, feel free to ask!

    Update 1:

    var obj = [
        {"slno":"1","productid":"10001","qty":"1","price":"250"},
        {"slno":"2","productid":"10005","qty":"1","price":"350"},
        {"slno":"3","productid":"10001","qty":"1","price":"250"},
        {"slno":"4","productid":"10003","qty":"1","price":"450"},
        {"slno":"5","productid":"10005","qty":"1","price":"350"}
    ];
    
    var aggregatedArray = [];
    
    obj.forEach(function(item) {
        var existingItem = aggregatedArray.find(function(aggregatedItem) {
            return aggregatedItem.productid === item.productid;
        });
    
        if (existingItem) {
            existingItem.qty = parseInt(existingItem.qty) + 1;
            existingItem.price = parseInt(existingItem.price) + parseInt(item.price);
        } else {
            aggregatedArray.push({
                slno: item.slno,
                productid: item.productid,
                qty: item.qty,
                price: item.price
            });
        }
    });
    
    var newArray = JSON.stringify(aggregatedArray);
    
    console.log(newArray);

    try this

    Login or Signup to reply.
  2. the product ids are in string format and needs a type assertion to int with ParseInt() function . you can index over them afterwards , add them and store in a new array . there are so many different of doing so with js builtin methods and functions , but i saw this one easy.

    var obj = [
    { "slno": "1", "productid": "10001", "qty": "1", "price": "250" },
    { "slno": "2", "productid": "10005", "qty": "1", "price": "350" },
    { "slno": "3", "productid": "10001", "qty": "1", "price": "250" },
    { "slno": "4", "productid": "10003", "qty": "1", "price": "450" },
    { "slno": "5", "productid": "10005", "qty": "1", "price": "350"            },
    ];
    
    var newArr = [];
    
    
    newArr.push(parseInt(obj[0].productid) + parseInt(obj[1].productid));
    
    console.log(newArr);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search