EN VI

Jquery - adding json data value?

2024-03-11 15:00:07
How to Jquery - adding json data value

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"}]";

Solution:

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

Answer

Login


Forgot Your Password?

Create Account


Lost your password? Please enter your email address. You will receive a link to create a new password.

Reset Password

Back to login