Sai Charan
Sai Charan

Reputation: 153

How to specify array elements as property values in an object in JavaScript?

Lets say I have an array

var array = ["lionel", "messi"];

I want to use the values of the elements of that array for property values in an object.

var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
}

I want the final object to be,

var obj = {
  "first-name": "lionel",
  "last-name": "messi"
}

How do I do this in JavaScript? Or is there a lodash function which can do this?

Upvotes: 0

Views: 79

Answers (4)

T.J. Crowder
T.J. Crowder

Reputation: 1074248

You just assign to those properties:

obj["first-name"] = array[0];
obj["last-name"] = array[1];

Example:

var array = ["lionel", "messi"];
var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
};
obj["first-name"] = array[0];
obj["last-name"] = array[1];
console.log(obj);

In ES2015+, you can use a destructuring assignment:

[obj["first-name"], obj["last-name"]] = array;

Example:

var array = ["lionel", "messi"];
var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
};
[obj["first-name"], obj["last-name"]] = array;
console.log(obj);


In a comment you've said:

but what if I had to do it dynamically? How do I loop over both in the array/object?

If you mean that you want to assign the entries from array to the object properties in obj by their position, beware that object property order is new as of ES2015 and it's easily misunderstood.

But with a fully ES2015+ compliant JavaScript engine, if your object really is defined as:

var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
};

...then Object.getOwnPropertyNames will reliably return ["first-name", "last-name"] (Object.keys is not guaranteed to), because:

  1. The properties were created in that order by the object initializer
  2. They're "own" properties
  3. They have string names (not Symbol names),
  4. They don't look like array indexes, and
  5. It's an ordinary object

Yes, that's quite a lot of caveats. So you could use that, like this:

Object.getOwnPropertyNames(obj).forEach((key, index) => {
    obj[key] = array[index];
});

Example:

var array = ["lionel", "messi"];
var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
};
Object.getOwnPropertyNames(obj).forEach((key, index) => {
    obj[key] = array[index];
});
console.log(obj);

Again: That only works on a fully ES2015+ compliant JavaScript engine, with an object defined as shown above. The order in which properties are reported by getOwnPropertyNames is potentially complex; details in the spec.

Upvotes: 5

fresh mouse
fresh mouse

Reputation: 382

This will work:

var array = ["lionel", "messi"];
var order = ["first-name", "last-name"];

var obj = {
  "first-name": "cristiano",
  "last-name": "ronaldo"
}

for(var i = 0; i < array.length; i++) {
  obj[order[i]] = array[i];
}

console.log(obj);

Every thing is pretty self-explanatory, except for the Object.keys(obj) part. It just returns an array of the keys of obj(`["first-name", "last-name"]) it gets each one and assigns them to you array.

EDIT:

Also, as tibetty said in the comment, you need an order array.

Upvotes: 1

Ankit Vishwakarma
Ankit Vishwakarma

Reputation: 1691

Javascript has no concept of associative array like PHP. You can create JSON (Javascript Object Notation). And please try to understand the concept of using object. Don't use object because you want to use it like array. Javascript has the concept of classes. You can create class having member first name , last name. Instance it and assign your values.

Upvotes: -1

tibetty
tibetty

Reputation: 563

You can implement as:

function heteroCopy(obj, array) {
    let keys = Object.keys(obj);
    for (let i in keys) {
        obj[keys[i]] = array[i];
    }
    return obj;
}

Upvotes: 1

Related Questions