1

I searched online and read up on the Lodash documentation, but could not find a quick fix to this.

Basically, I have two arrays

var employee = ["John", "Mary"];
var location = ["APAC", "singapore"];

And eventually I would like to have a Json object that look like this:

var obj = {
   "profile": [
    {
      "name": "John",
      "location": ["APAC","Singapore"]
    },
    {
      "name": "Mary",
      "location": ["APAC","Singapore"]
    }
   ]
}
3
  • why is "Mary" missed? Commented Mar 1, 2017 at 15:06
  • 1
    What have you already tried? Commented Mar 1, 2017 at 15:06
  • made a correction : I looked at _.zipObject and _.castArray, but have yet to figure out how to add "profile", "name" or "location" into the object Commented Mar 1, 2017 at 15:12

5 Answers 5

3

Don't use location as variable name (var location ...) because it will conflict with window.location object.

The solution using Array.prototype.reduce() function:

var employee = ["John", "Mary"],
    loc = ["APAC", "Singapore"],
    result = employee.reduce(function (r, e) {
        r.profile.push({"name": e, "location": loc.slice()});
        return r;
    }, {profile: []});

console.log(result);

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

7 Comments

This a really nice way of doing it!
reduce is just a waste! map is better!
This is really a neat way of doing it without using a for loop, thanks!
Also a quick question, why do you use loc.slice()? I dont see the difference in array if you just push loc
@hao, Javascript arrays are passed by reference, you should use Array.prototype.slice() to push a copy of the initial array(to avoid further modifications)
|
3

You could use _.zipWith with lodash:

var employee = ["John", "Mary"];
var location = ["APAC", "singapore"];
var output = {
  profile: _.zipWith(employee, name =>  ({
    name,
    location
  }))
};

console.log(output);

Comments

0

To achieve the above you could do the following using ES2015:

const employees = ["John", "Mary"];
const locations = ["APAC", "singapore"];

const final = []

employees.forEach( (employee) => {
  let obj = {
    name: employee,
    location: [...locations]
  }
  final.push(obj)
})

Comments

0

Maybe a simple loop will do the trick :

var obj = { "profile" : [] };
employee.forEach(function(e) {
    obj.profile.push({ "name": e, "location": location });
});

2 Comments

I believe the location would not work using this solution as you would get a circular object you could fix this by using the ES2015 spread operator [...location]
It works, but he needs to rename the var location as it will conflict with window.location, as mentioned in an answer before.
0

Here is a quick and dirty solution using map and node:

var obj = {
  "profile": employees.map((name) => {
    return {
      "name": name,
      "location": location
    };
  })
};

2 Comments

map returns a new array, not changing the original!
Plus the whole location array should be attached to all the employees not just the one at the same index!

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.