亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Javascript - 如果對象不存在,則將其添加到數組中,否則增加數量

Javascript - 如果對象不存在,則將其添加到數組中,否則增加數量

慕森王 2022-10-13 15:32:16
如果對象不在本地存儲數組中,我想將它們添加到本地存儲數組中,否則將對象的數量增加 1。這是我的功能:function addToCart() {    let productsTable = localStorage.getItem("productList");    // Check if productsTable exists in local storage    if (productsTable === null){        // If not, initialize the array and add the current object         productsTable = [];         objetProduit.quantity ++;         productsTable.push(objetProduit);    }else{        // If yes, decode the array.         productsTable = JSON.parse(productsTable);        // check if the object is already in the array        if(productsTable.find(product => product.id !== objetProduit.id)){                    //if not ==> add the object into the array            objetProduit.quantity ++;            productsTable.push(objetProduit);        }else if (productsTable.find(product => product.id == objetProduit.id)){            //if yes ==> just increase the value of the key quantity by 1            objetProduit.quantity ++;        }    }    // Encode the array.    productsTable = JSON.stringify(productsTable);    // Add the array back to LocalStorage.     localStorage.setItem("productList", productsTable);objetcs 是以下類的實例:class Appareil {    constructor(id,name,price,description,imageURL, quantity){        this.id = id;        this.name = name;        this.price = price;        this.description = description;        this.imageURL = imageURL;        this.quantity = quantity;    }}數組已初始化,但未在其中添加項目,也未修改數量。
查看完整描述

1 回答

?
MM們

TA貢獻1886條經驗 獲得超2個贊

避免以下檢查條件:


if(productsTable.find(product => product.id !== objetProduit.id))

因為它將返回數組中不匹配的第一個產品的索引,所以很可能會有很多產品不匹配


參考:

https ://developer.mozilla.org/en-US/docs/Web/ JavaScript/參考/Global_Objects/數組/查找


嘗試:


function addToCart() {

    let productsTable = localStorage.getItem("productList");


    // Check if productsTable exists in local storage

    if (!productsTable) {


        // If not, initialize the array and add the current object

        productsTable = [];

        objetProduit.quantity++;

        productsTable.push(objetProduit);

    } else {


        // If yes, decode the array. 

        productsTable = JSON.parse(productsTable);


        // check if the object is already in the array

        if (productsTable.find(product => product.id === objetProduit.id)) {


            //if yes ==> just increase the value of the key quantity by 1

            objetProduit.quantity++;

            for (var i = 0; i < productsTable.length; i++) {

                if (objetProduit.id === productsTable[i].id) { //look for match with id

                    productsTable[i].quantity++; //add

                    break; //exit loop

                }

            }

        } else {

            //if not ==> add the object into the array

            objetProduit.quantity++;

            productsTable.push(objetProduit);


        }

    }

    // Encode the array.

    productsTable = JSON.stringify(productsTable);


    // Add the array back to LocalStorage. 

    localStorage.setItem("productList", productsTable);

}


查看完整回答
反對 回復 2022-10-13
  • 1 回答
  • 0 關注
  • 317 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號