如何在javascript中将值插入数组

时间:2018-06-29 01:31:08

标签: javascript arrays json

如果值匹配,我想将值从一个数组插入到另一个数组。

这些是我的数组。

var names = [{key: 'Apple'}, {key: 'Dell'}, {key: 'Xitrix'}];
var data = [
     {enum_name:'Apple', date:'2000-12-15', accomp: 12},
     {enum_name:'Dell', date:'2000-12-15', accomp: 8},
     {enum_name:'Apple', date:'2000-12-16', accomp: 12},
     {enum_name:'Xitrix', date:'2000-12-16', accomp: 12},
     {enum_name:'Dell', date:'2000-12-17', accomp: 8},
     {enum_name:'Xitrix', date:'2000-12-17', accomp: 12},
];

然后我想使用特定键(例如attrib:[arrays];

)将data.date和data.accomp插入数组 名称

我有这段代码,但是它没有将所有匹配的数据插入 attrib

    var counts = 0;
    _.each(names, function(v, k){
        _.each(data, function(val, key){
            if(v.key == val.enum_name){
                var date = val.date;
                var accomp= val.accomp;

                names[k].attrib= [[date, accomp]];
                console.log(counts);
                counts++;
            }
        });
        counts = 0;
        //names[k]['values'] = 'test';
    });

我想要这样的输出。

 names = [
    {key: 'Apple', attrib: [['2000-12-15', 12], ['2000-12-16', 12]]}, 
    {key: 'Dell', attrib: [['2000-12-15', 8], ['2000-12-17', 8]]},
    {key: 'Xitrix', attrib: [['2000-12-16', 12], ['2000-12-17', 12]]}
 ];

3 个答案:

答案 0 :(得分:3)

尝试将reduce用于由enum_name索引并带有dateaccomp数组的对象,然后获取该对象的值,不需要库,并且运行时复杂度更低:

var names = [{key: 'Apple'}, {key: 'Dell'}, {key: 'Xitrix'}];
var data = [
   {enum_name:'Apple', date:'2000-12-15', accomp: 12},
   {enum_name:'Dell', date:'2000-12-15', accomp: 8},
   {enum_name:'Apple', date:'2000-12-16', accomp: 12},
   {enum_name:'Xitrix', date:'2000-12-16', accomp: 12},
   {enum_name:'Dell', date:'2000-12-17', accomp: 8},
   {enum_name:'Xitrix', date:'2000-12-17', accomp: 12},
];
console.log(Object.values(
  data.reduce((a, { enum_name: key, date, accomp }) => {
    if (!a[key]) a[key] = { key, attrib: [] };
    a[key].attrib.push([date, accomp]);
    return a;
  }, {})
));

答案 1 :(得分:1)

首先,创建一个包含names元素的对象,并以key属性为键,因此很容易找到特定键的元素,然后将attrib属性添加到他们。

Lodash:

var namesObj = _.forEach(_.keyBy(names, 'key'), n => n.attrib = []);

Undercore.js:

var namesObject = _.each(_.indexBy(names, 'key'), n => n.attrib = []);

然后,您可以遍历数据并将属性推送到适当的对象上。

_.each(data, ({enum_name, date, accomp}) => 
    namesObj[enum_name].attribs.push([date, accomp])
);

答案 2 :(得分:0)

Underscore where filter

Plz尝试输入以下代码:[]

  var names = [{ key: 'Apple' }, { key: 'Dell' }, { key: 'Xitrix' }];
  var data = [
    { enum_name: 'Apple', date: '2000-12-15', accomp: 12 },
    { enum_name: 'Dell', date: '2000-12-15', accomp: 8 },
    { enum_name: 'Apple', date: '2000-12-16', accomp: 12 },
    { enum_name: 'Xitrix', date: '2000-12-16', accomp: 12 },
    { enum_name: 'Dell', date: '2000-12-17', accomp: 8 },
    { enum_name: 'Xitrix', date: '2000-12-17', accomp: 12 }
  ];
  // new array
  var new_data = [];
  // loop names
  _.each(names, function (v) {
    // init new obj
    var obj = { key: v.key, attrib: [] };
    // find by underscore with key of names & enum_name of data
    var finds = _.where(data, { enum_name: v.key });
    // if using lodash then change to _.find:
    // var finds = _.find(data, { enum_name: v.key });
    // loop finds
    _.each(finds, function (value) {
      // add attrib
      obj.attrib.push([value.date, value.accomp]);
    });
    // add obj to new array
    new_data.push(obj);
  });
  console.log(new_data);