javascript:逐个将数组插入到对象中

时间:2018-03-20 05:54:16

标签: javascript ecmascript-6 ecmascript-7

我需要将位置,商品等数组插入一个对象,在这种情况下是refdata。我需要将数组插入对象逐个 ..我已经在下面的代码中完成了: -

但我没有得到所需的输出。任何帮助将不胜感激。



var refdata = {
    locations: [],
    commodities: []

}

var locations=[
    {
      "symbol": "IND",
      "name": "INDIA"
    }
  ]

 var commodities= [
    {
      "name": "Aluminium",
      "symbol": "AL"
    }
  ]
this.refdata={locations};
this.refdta={commodities};

console.log(this.refdata)




3 个答案:

答案 0 :(得分:0)

你可以push refdatalocations

的0个元素var refdata = { locations: [], commodities: [] } var locations = [{ "symbol": "IND", "name": "INDIA" }] var commodities = [{ "name": "Aluminium", "symbol": "AL" }] refdata.locations.push(locations[0]); refdata.commodities.push(commodities[0]); console.log(refdata);

var refdata = {
  locations: [],
  commodities: []

}

var locations = [{
  "symbol": "IND",
  "name": "INDIA"
}]

var commodities = [{
  "name": "Aluminium",
  "symbol": "AL"
}]


refdata.locations = locations;
refdata.commodities = commodities;

console.log(refdata);

或者直接指定它来覆盖值。

 $(document).ready(function() {
    load_content();
    function load_content(){
            var total_record = 0;
            var total_groups = <?php echo $total_data; ?>;  
        //brand is the checkbox value
            var brand=check_box_values('brand');
            $('#results').load("<?php echo base_url() ?>content/load_more",
             {'group_no':total_record,'brand':brand}, function() {total_record++;});        
     }
        //For passing checkbox values
             function check_box_values(check_box_class){
                        var values = new Array();
                            $("."+check_box_class).each(function() {
                              if($(this).is(':checked')){
                               values.push($(this).val());
                                }
                            });
                        return values;
                    }
        $(".brand").click(function(){
          load_content();
       });

$(window).scroll(function() {       
                if($(window).scrollTop()+$(window).height() >= $('#fooerdivid').offset().top)  
                {           
                   var total_record = 0;
            var total_groups = <?php echo $total_data; ?>;  
        //brand is the checkbox value
            var brand=check_box_values('brand');

                    if(total_record <= total_groups)
                    {
                      loading = true; 
                      $('.loader_image').show(); 
                      $.post('<?php echo site_url() ?>content/load_more',{'group_no': total_record,'brand':brand},
                        function(data){ 
                            if (data != "") {                               
                                $("#results").append(data);                 
                                $('.loader_image').hide();                  
                                total_record++;
                            }
                        });     
                    }
                }
            });
    });

答案 1 :(得分:0)

在ES6中,您可以使用简写语法:

&#13;
&#13;
let locations = [{
  "symbol": "IND",
  "name": "INDIA"
}]

let commodities = [{
  "name": "Aluminium",
  "symbol": "AL"
}]

let refdata = { locations, commodities }

console.log(refdata)
&#13;
&#13;
&#13;

let refdata = { locations, commodities }

的缩写
let refdata = { locations: locations, commodities: commodities }

如果您需要逐个执行此操作,如问题中所述,您可以使用Object.assign()

&#13;
&#13;
let refdata = {}

let locations = [{
  "symbol": "IND",
  "name": "INDIA"
}]

refdata = Object.assign(refdata, { locations })

let commodities = [{
  "name": "Aluminium",
  "symbol": "AL"
}]

refdata = Object.assign(refdata, { commodities })

console.log(refdata)
&#13;
&#13;
&#13;

提示:我已添加此答案,因为您标记了问题

答案 2 :(得分:0)

由于您希望逐项插入refdata位置数组。我想你想对每个项目进行一些操作。如果是这样,以下代码将是合适的。

var refdata = {
    locations: [],
    commodities: []
};

var locations=[
    {
      "symbol": "IND",
      "name": "INDIA"
    }
  ];

 var commodities= [
    {
      "name": "Aluminium",
      "symbol": "AL"
    }
  ];
  
 for (loc of locations) {
    // Do your object manipulation of loc if required
    console.log(loc);
    refdata.locations.push(loc);
 };
 for (commodity of commodities) {
    // Do your object manipulation of commodity if required
    console.log(commodity);
    refdata.commodities.push(commodity);
 };
 
console.log(refdata);