0

Lodash v 4.17.15

Let's say I have 2 array

var users = [{
      id: 12,
      name: Adam
   },{
      id: 14,
      name: Bob
   },{
      id: 16,
      name: Charlie
   },{
      id: 18,
      name: David
   }
]


var jobs = [{
      empid: 12,
      profession: Engineer
   },{
      empid: 14,
      profession: CEO
   },{
      empid: 16,
      profession: CFO
   },{
      empid: 18,
      profession: CTO
   }
]

Expected Result

var jobsEmp= [{
          empid: 12,
          id: 12,
          name: Adam
          profession: Engineer
       },{
          empid: 14,
          id: 14,
          name: Bob
          profession: CEO
       },{
          empid: 16,
         id: 16,
          name: Charlie
          profession: CFO
       },{
          empid: 18,
          id: 18,
          name: David
          profession: CTO
       }
    ]

I usually do this :

var jobsEmp =  _.forEach(users, function (emp) {
                    emp.jobs = _.find(jobs, function (jb) {
                        return jb.empid === emp.id;
                    });
                });

But I've noticed that it's not the best way, I've been reading the doc for quite a while, but I just don't know what technique is the best, how to do it properly in lodash?

2 Answers 2

2

You can do like this but IMO, whatever make it more readable for you and your team. i would prefer method with higher readability

const _ = require('lodash')

const users = [{
      id: 12,
      name: 'Adam'
   },{
      id: 14,
      name: 'Bob'
   },{
      id: 16,
      name: 'Charlie'
   },{
      id: 18,
      name: 'David'
   }
]


const jobs = [{
      empid: 12,
      profession: 'Engineer'
   },{
      empid: 14,
      profession: 'CEO'
   },{
      empid: 16,
      profession: 'CFO'
   },{
      empid: 18,
      profession: 'CTO'
   }
]

const merged = _.merge(_.keyBy(users, 'id'), _.keyBy(jobs, 'empid'));
const values = _.values(merged);
console.log(values);

output

[ { id: 12, name: 'Adam', empid: 12, profession: 'Engineer' },
  { id: 14, name: 'Bob', empid: 14, profession: 'CEO' },
  { id: 16, name: 'Charlie', empid: 16, profession: 'CFO' },
  { id: 18, name: 'David', empid: 18, profession: 'CTO' } ]
Sign up to request clarification or add additional context in comments.

Comments

1

Without using lodash:

const users = [{
  id: 12,
  name: "Adam"
}, {
  id: 14,
  name: "Bob"
}, {
  id: 16,
  name: "Charlie"
}, {
  id: 18,
  name: "David"
}];


const jobs = [{
  empid: 12,
  profession: "Engineer"
}, {
  empid: 14,
  profession: "CEO"
}, {
  empid: 16,
  profession: "CFO"
}, {
  empid: 18,
  profession: "CTO"
}];

const result = users.map((user) => {
  const job = jobs.find((job) => job.empid === user.id);

  return Object.assign({}, user, job);
});

console.log(result);

1 Comment

That's the same as the OP wrote with lodash, and the question was: is there a better way?

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.