Adding elements to object

前端 未结 17 2100
有刺的猬
有刺的猬 2020-12-02 04:11

I need to populate a json file, now I have something like this:

{\"element\":{\"id\":10,\"quantity\":1}}

And I need to add another \"elemen

相关标签:
17条回答
  • 2020-12-02 04:36

    If anyone comes looking to create a similar JSON, just without using cart as an array, here goes:

    I have an array of objects myArr as:

    var myArr = [{resourceType:"myRT",
                id: 1,
                value:"ha"},
                {resourceType:"myRT",
                id: 2,
                value:"he"},
                {resourceType:"myRT",
                id: 3,
                value:"Li"}];
    

    and I will attempt to create a JSON with the following structure:

    {
     "1":{"resourceType":"myRT","id":"1","value":"ha"},
     "2":{"resourceType":"myRT","id":"2","value":"he"},
     "3":{"resourceType":"myRT","id":"3","value":"Li"}
    }
    

    you can simply do-

    var cart = {};
    myArr.map(function(myObj){
                        cart[myObj.id]= myObj;
                        });
    
    0 讨论(0)
  • 2020-12-02 04:38
     function addValueInObject(value, object, key) {
    
            var addMoreOptions = eval('{"'  + key + '":' +  value + '}');
    
            if(addMoreOptions != null) {
                var textObject = JSON.stringify(object);
                textObject = textObject.substring(1,textObject.length-1);
                var AddElement = JSON.stringify(addMoreOptions);
                object = eval('{' + textObject +','+  AddElement.substring(1,AddElement.length-1) + '}');
            }
            return object;
        }
    
    addValueInObject('sdfasfas', yourObject, 'keyname');
    

    OR:

    var obj = {'key':'value'};
    
    obj.key2 = 'value2';
    
    0 讨论(0)
  • 2020-12-02 04:41

    For anyone still looking for a solution, I think that the objects should have been stored in an array like...

    var element = {}, cart = [];
    element.id = id;
    element.quantity = quantity;
    cart.push(element);
    

    Then when you want to use an element as an object you can do this...

    var element = cart.find(function (el) { return el.id === "id_that_we_want";});
    

    Put a variable at "id_that_we_want" and give it the id of the element that we want from our array. An "elemnt" object is returned. Of course we dont have to us id to find the object. We could use any other property to do the find.

    0 讨论(0)
  • 2020-12-02 04:42

    My proposition is to use different data structure that proposed already in other answers - it allows you to make push on card.elements and allow to expand card properties:

    let card = {
      elements: [
        {"id":10,"quantity":1}
      ],
    
      //other card fields like 'owner' or something...
    }
    
    card.elements.push({"id":22,"quantity":3})
    
    console.log(card);

    0 讨论(0)
  • 2020-12-02 04:43
    cart.push({"element":{ id: id, quantity: quantity }});
    
    0 讨论(0)
提交回复
热议问题