Updating a JSON object using Javascript

前端 未结 9 1544
Happy的楠姐
Happy的楠姐 2020-12-07 13:37

How can i update the following JSON object dynamically using javascript or Jquery?

var jsonObj = [{\'Id\':\'1\',\'Username\':\'Ray\',\'FatherName\':\'Thompso         


        
9条回答
  •  再見小時候
    2020-12-07 13:57

    For example I am using this technique in Basket functionality.

    Let us add new Item to Basket.

    var productArray=[];
    
    
    $(document).on('click','[cartBtn]',function(e){
      e.preventDefault();
      $(this).html('Added to cart');
      console.log('Item added ');
      var productJSON={"id":$(this).attr('pr_id'), "nameEn":$(this).attr('pr_name_en'), "price":$(this).attr('pr_price'), "image":$(this).attr('pr_image'), "quantity":1, "discount":0, "total":$(this).attr('pr_price')};
    
    
      if(localStorage.getObj('product')!==null){
        productArray=localStorage.getObj('product');
        productArray.push(productJSON);  
        localStorage.setObj('product', productArray);  
      }
      else{
        productArray.push(productJSON);  
        localStorage.setObj('product', productArray);  
      }
    
    
      itemCountInCart(productArray.length);
    
    });
    

    After adding some item to basket - generates json array like this

    [
        {
            "id": "95",
            "nameEn": "New Braslet",
            "price": "8776",
            "image": "1462012394815.jpeg",
            "quantity": 1,
            "discount": 0,
            "total": "8776"
        },
        {
            "id": "96",
            "nameEn": "new braslet",
            "price": "76",
            "image": "1462012431497.jpeg",
            "quantity": 1,
            "discount": 0,
            "total": "76"
        },
        {
            "id": "97",
            "nameEn": "khjk",
            "price": "87",
            "image": "1462012483421.jpeg",
            "quantity": 1,
            "discount": 0,
            "total": "87"
        }
    ]
    

    For Removing some item from Basket.

    $(document).on('click','[itemRemoveBtn]',function(){
    
    var arrayFromLocal=localStorage.getObj('product');
    findAndRemove(arrayFromLocal,"id",$(this).attr('basketproductid'));
    localStorage.setObj('product', arrayFromLocal);
    loadBasketFromLocalStorageAndRender();
    });
    
    //This function will remove element by specified property. In my case this is ID.
    function findAndRemove(array, property, value) {
      array.forEach(function(result, index) {
        if(result[property] === value) {
          //Remove from array
          console.log('Removed from index is '+index+' result is '+JSON.stringify(result));
          array.splice(index, 1);
    
        }    
      });
    }
    

    And Finally the real answer of the question "Updating a JSON object using JS". In my example updating product quantity and total price on changing the "number" element value.

    $(document).on('keyup mouseup','input[type=number]',function(){
    
      var arrayFromLocal=localStorage.getObj('product');
      setQuantityAndTotalPrice(arrayFromLocal,$(this).attr('updateItemid'),$(this).val());
      localStorage.setObj('product', arrayFromLocal);
      loadBasketFromLocalStorageAndRender();
    });
    
    function setQuantityAndTotalPrice(array,id,quantity) {
    
      array.forEach(function(result, index) {
        if(result.id === id) {
          result.quantity=quantity;
          result.total=(quantity*result.price);
        }    
      });
    
    }
    

提交回复
热议问题