0

I have an array of arrays which looks like below:

UserList=[
[name:"user1", type:"admin", location:"NY", expired:"NO"],
[name:"user2", type:"poweruser", location:"CO", expired:"NO"],
[name:"user3", type:"admin", location:"SF", expired:"NO"],
]

I want to add the three properties name, type and location and create a new property in each individual array like the property "AllProps" in the below example:

Desired Output:

UserList=[
[name:"user1", type:"admin", location:"NY", expired:"NO",AllProps:"user1adminNY"],
[name:"user2", type:"poweruser", location:"CO", expired:"NO",AllProps:"user1poweruserCO"],
[name:"user3", type:"admin", location:"SF", expired:"NO", AllProps:"user1adminSF"],
]

Can I do this using Loadash? What is the best and fastest way to do this?

3 Answers 3

2

You are using {} but it should be [] for array. Also you need to use : instead of = in objects

var userList= [
{name: "user1", type: "admin", location: "NY", expired: "NO"},
{name: "user2", type: "poweruser", location: "CO", expired: "NO"},
{name: "user3", type: "admin", location: "SF", expired: "NO"}
];

var output = userList.map(user => {
   user.AllProps = user.name + user.type + user.location;
   return user;
});

// Short hand
var output = userList.map(user => ({ ...user, AllProps: user.name + user.type + user.location}));

console.log('output:', output);

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

Comments

1

Based on your requirements and wanting to just modify your existing array without return a new one, simply iterate the array and add the key/value pair:

var UserList = [
	{name: "user1", type: "admin", location: "NY", expired: "NO"},
	{name: "user2", type: "poweruser", location: "CO", expired: "NO"},
	{name: "user3", type: "admin", location: "SF", expired: "NO"},
];

UserList.forEach(function(x){
	x["AllProps"] = x.name + x.type + x.location;
});	

console.log(UserList);

The upvoted answer will work fine, but there is no need to return anything.

Comments

1

I think something like this is that you need:

_.forEach(UserList, function(u){
  var allPropValues = "";
  _.forOwn(u, function(value, key) {
     if(key !== 'expired') {
        allPropValues += value;
     }
  });  
  u.AllProps = allPropValues;
});

var UserList=[
{name:"user1", type:"admin", location:"NY", expired:"NO"},
{name:"user2", type:"poweruser", location:"CO", expired:"NO"},
{name:"user3", type:"admin", location:"SF", expired:"NO"},
];
console.log({before: UserList});

_.forEach(UserList, function(u){
  var allPropValues = "";
  _.forOwn(u, function(value, key) {
     if(key !== 'expired') {
        allPropValues += value;
     }
  });  
  u.AllProps = allPropValues;
});

console.log({after: UserList});
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.9/lodash.js"></script>

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.