猿问

Javascript - 如果对象不存在,则将其添加到数组中,否则增加数量

如果对象不在本地存储数组中,我想将它们添加到本地存储数组中,否则将对象的数量增加 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;


    }

}

数组已初始化,但未在其中添加项目,也未修改数量。


慕森王
浏览 280回答 1
1回答

MM们

避免以下检查条件:if(productsTable.find(product => product.id !== objetProduit.id))因为它将返回数组中不匹配的第一个产品的索引,所以很可能会有很多产品不匹配参考:https ://developer.mozilla.org/en-US/docs/Web/ JavaScript/参考/Global_Objects/数组/查找尝试:function addToCart() {&nbsp; &nbsp; let productsTable = localStorage.getItem("productList");&nbsp; &nbsp; // Check if productsTable exists in local storage&nbsp; &nbsp; if (!productsTable) {&nbsp; &nbsp; &nbsp; &nbsp; // If not, initialize the array and add the current object&nbsp; &nbsp; &nbsp; &nbsp; productsTable = [];&nbsp; &nbsp; &nbsp; &nbsp; objetProduit.quantity++;&nbsp; &nbsp; &nbsp; &nbsp; productsTable.push(objetProduit);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; // If yes, decode the array.&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; productsTable = JSON.parse(productsTable);&nbsp; &nbsp; &nbsp; &nbsp; // check if the object is already in the array&nbsp; &nbsp; &nbsp; &nbsp; if (productsTable.find(product => product.id === objetProduit.id)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if yes ==> just increase the value of the key quantity by 1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objetProduit.quantity++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (var i = 0; i < productsTable.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (objetProduit.id === productsTable[i].id) { //look for match with id&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productsTable[i].quantity++; //add&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break; //exit loop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //if not ==> add the object into the array&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; objetProduit.quantity++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; productsTable.push(objetProduit);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; // Encode the array.&nbsp; &nbsp; productsTable = JSON.stringify(productsTable);&nbsp; &nbsp; // Add the array back to LocalStorage.&nbsp;&nbsp; &nbsp; localStorage.setItem("productList", productsTable);}
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答