【问题标题】:JSON add nested sub objects dynamically using JS or JQueryJSON使用JS或JQuery动态添加嵌套子对象
【发布时间】:2022-01-13 19:28:59
【问题描述】:

我正在客户端创建服务车 其中服务被分组在 3 个级别的分组中,例如

product[SECTION] [SERVICE]   [ITEM]
product['carpet']['cleaning']['room']   = {'qty':2,'price':15};
product['carpet']['cleaning']['hall']   = {'qty':1,'price':10};
product['carpet']['protecting']['hall'] = {'qty':1,'price':10}; 
product['carpet']['deodorize']['hall']  = {'qty':1,'price':10};
product['leather']['cleaning']['sofa']  = {'qty':1,'price':10};

想生成上面的json结构。

我的文本框如下所示:data-sectiondata-servicedata-temdata-price

<input type="text" class="form-control input-number" 
       data-price="15" data-section="carpet" data-item="room" data-service="protect" />

我的 JS 代码如下,但它只添加当前项目,同时覆盖所有其他服务和部分。

$(function(){
    $('.input-number').change(function(){
        var section = $(this).attr('data-section');
        var item = $(this).attr('data-item');
        var service = $(this).attr('data-service');
        var qty = $(this).val();
        var unitprice = $(this).attr('data-unitprice'); 

        addProduct(section,item,service,qty,unitprice);
    });
});

function addProduct(section,item,service,qty,unitprice){
    let products = {};
    if(localStorage.getItem('products')){
        products = JSON.parse(localStorage.getItem('products'));
    }

    products['"'+section+'"'] =
    { 
        ['"'+service+'"'] : {
            ['"'+item+'"'] : {
                'unitprice' : unitprice, 'qty': qty
            }
        } 
    }; 
    
    localStorage.setItem('products', JSON.stringify(products));
}

我怎样才能只追加而不是覆盖嵌套项?

已编辑 我已经编辑了我的添加产品功能,但仍然没有得到想要的结果

function addProduct(section,item,service,qty,unitprice){
    let products = {};
    if(localStorage.getItem('products')){
        products = JSON.parse(localStorage.getItem('products'));
    }
    

    var v = {
        [service] : {
            [item] :{
                "qty":qty,'unitprice':unitprice
            }
        }
    };

    products.push( section, v );

             
    localStorage.setItem('products', JSON.stringify(products));
}

Object.prototype.push = function( key, value ){
    if(typeof value === 'object'){ 
        var k = Object.keys(value)[0];
        value.push( k, value[k] );
    }
    this[ key ] = value;
    return this;
}

【问题讨论】:

    标签: json local-storage key-value


    【解决方案1】:

    首先,这不是存储数据的好方法。

    这是存储数据的更好方法。 如果您将其视为对象,则更容易理解,因为在 javascript 中它是同一件事

    //example this 2 data is store the same way in javascript
    var product['carpet']['cleaning']['room']   = {'qty':2,'price':15};
    
    var product = [{
      carpet: {
        cleaning: {
          room: {'qty':2,'price':15}
        }
      }
    }]
    
    // my solution would be just save section, service and item in the object, example:
    var product = [];
    var v = {
      section: 'carpet',
      service: 'cleaning',
      item: 'room'
      qty:2,
      price:15
    }
    product.push(v)
    

    【讨论】:

      猜你喜欢
      • 2021-10-06
      • 2017-04-28
      • 1970-01-01
      • 2016-09-09
      • 1970-01-01
      • 2016-12-27
      • 1970-01-01
      • 2016-12-23
      • 2011-12-13
      相关资源
      最近更新 更多