Reputation: 153
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
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:
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
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
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
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