1

I want to insert the values from one array to another array if the values matched.

These are my arrays.

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},
];

Then i want to insert data.date and data.accomp into the array names with a specific key (e.g. attrib: [arrays];

I have this code but it doesn't insert all the matched data to the key 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';
    });

i want an output like this.

 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]]}
 ];
4
  • 2
    {'2000-12-17', 12} That is invalid syntax, what do you actually want there? Commented Jun 29, 2018 at 1:35
  • There's no jQuery here. You're using either underscore.js or lodash. Please edit the question to specify. Commented Jun 29, 2018 at 1:41
  • In {date, accomplished} did you mean accomp? Commented Jun 29, 2018 at 1:42
  • edited im sorry Commented Jun 29, 2018 at 1:47

3 Answers 3

3

Try using reduce into an object indexed by enum_name with an array of dates and accomps, and then get that object's values, no library needed, and with less runtime complexity:

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;
  }, {})
));

Sign up to request clarification or add additional context in comments.

Comments

1

First start by creating an object containing the names elements, keyed by the key property, so it's easy to find the element for a specific key, and add the attrib property to them.

Lodash:

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

Underscore.js:

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

Then you can loop over the data and push the attributes onto the appropriate object.

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

3 Comments

is there a _.keyBy function in underscore?
it's called _.indexBy in underscore.js.
names is Object cannot be .forEach
0

Underscore where filter

Plz Try this code: []

  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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.