JavaScript-asking questions about implementing an array of shopping carts!!!!!!

Source: Internet
Author: User
Each product has ID, title, price, every click on the purchase I will add it to the array, but I want to judge by ID, when adding a product to the shopping cart, add the same product to a two-dimensional array, how to write it?

[    [        {鸡腿},{鸡腿},{鸡腿},{鸡腿}    ],    [        {狗腿},{狗腿},{狗腿}    ],    [        {猫咪},{猫咪},{猫咪},{猫咪},{猫咪},{猫咪}    ]]

Thank you, indeed I think this is really hot chicken, thank you for your advice!

Reply content:

Each product has ID, title, price, every click on the purchase I will add it to the array, but I want to judge by ID, when adding a product to the shopping cart, add the same product to a two-dimensional array, how to write it?

[    [        {鸡腿},{鸡腿},{鸡腿},{鸡腿}    ],    [        {狗腿},{狗腿},{狗腿}    ],    [        {猫咪},{猫咪},{猫咪},{猫咪},{猫咪},{猫咪}    ]]

Thank you, indeed I think this is really hot chicken, thank you for your advice!

The above friends are right, this idea of yours is not desirable in itself. The two-dimensional array is all the same thing. A bit of a pit.
This format is better, refer to

var cart = {    'id01':{n:'鸡腿', count: 4},    'id02':{n:'鸭腿', count: 3},    'id03':{n:'猪腿', count: 2},    'id04':{n:'狗腿子', count: 1}}

But if you're going to do it this way,

var list = [    [{n:'鸡腿'},{n:'鸡腿'},{n:'鸡腿'},{n:'鸡腿'},{n:'鸡腿'}],    [{n:'鸭腿'},{n:'鸭腿'},{n:'鸭腿'},{n:'鸭腿'}],    [{n:'猪腿'},{n:'猪腿'},{n:'猪腿'},{n:'猪腿'}]];function fn(o){    var inArray = false;    list.map(function(item){        if( item.indexOf(o) > -1){            inArray = true;            item.push(o);        }    });    inArray || list.push([o]);}fn(list[1][1]);fn({n:'狗腿子'});

This is only a reference, not advocating

This is not recommended, the data structure of the shopping cart should be stored in the ID and quantity of the product (assuming the product ID is the name)

{    "鸡腿": 4,    "狗腿": 3,    "猫咪": 6}

Concrete implementation of the time, the shopping cart items increase or decrease, as long as the back of this number to add and subtract it can be

You're not thinking that way. The main body of the shopping cart should be $a = ['id'=>number] , then the price and the name should be another array $b = ['id'=>['name'=>name,'price'=>price]] , the total price is$totalPrice = $a['id']*$b['id']['price']

Agree with the upstairs view, the contents of the shopping cart should be a total, but the price of the individual think should not be stored in the shopping cart array, because you join the price and the price is not necessarily the same time. Stored should be unique identifier ID and quantity number, if you store the title, price, if the merchant changed the name or price, your design how to deal with it!

$shoppingCart = [    '101' => 4,//鸡腿    '102' => 5,//狗腿    '103' => 6//鸭腿];

First, thank you for your invitation.

In fact, I saw this problem yesterday, I also think the upstairs several answers are good. But the Lord invited me again, only to publish my views, as a solicitation.

In fact, I think a few dimensional array is not important, how to write unimportant, it is important to train of thought. I am mainly making a presentation on this.
In fact, in this age, object-oriented although it is a cliché of things, but many people are still not very likely to use, so the complexity of the problem straight up, we try to solve the problem with the object-oriented to try it:

//全局对象var item_arr = {};//操作函数function add_(id, name, price, count) {    var item = {        id: id,        name: name,        price: price,        count: count    }    var obj = item_arr[id]    if (obj) {        item.count = obj.count + count;    }    item_arr[item.id] = item;}//code by rozbo ,强力免山寨//模拟添加购物操作add_(19, "狗腿子", 16, 20);add_(1, "鸡腿子", 12, 2);add_(126, "羊腿子", 6, 6);add_(126, "羊腿子", 6, 6);//输出信息,计算价格var price_totle = 0;for (var id in item_arr) {    var item = item_arr[id];    var price_curr = item.count * item.price;    price_totle += price_curr;    console.info("当前有%s%d个,总价%d元", item.name, item.count, price_curr);}console.info("共计%d元,祝您购物愉快!", price_totle);

Output results

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.