0

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?

3
  • 2
    Is there any structure to the array? Can we assume anything about the length of the array and the structure of the object, the variables, etc? Commented Aug 16, 2017 at 10:21
  • Is it really an array of objects your are looking for here? Commented Aug 16, 2017 at 10:33
  • @T.J.Crowder Did it, thank you! :) Commented Aug 16, 2017 at 11:50

4 Answers 4

5

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.

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

5 Comments

but what if I had to do it dynamically? How do I loop over both in the array/object?
@SaiCharan: Loop over what? You have only one entry in what you've shown. An array defining data for Lionel Messi, and an object with room to hold one person's data. If you have multiple of these, you need to show us that. For one thing, the structure of your object will be quite different.
@SaiCharan: If you mean that array[0] should be assigned to the "first" property of obj, I've added to the answer to explain how to do that on a fully ES2015+ compliant JavaScript engine. You cannot reliably do it on a pre-ES2015 engine.
thank you so much! Object.getOwnPropertyNames(obj).forEach((key, index) => { obj[key] = array[index]; }); this is the answer I was looking for. :)
@SaiCharan: No worries! Just do take note of the caveats, that won't work on IE11 and earlier for instance (and there's no other reliable way, cross-browser; you'd have to make your own array of property names).
1

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;
}

2 Comments

The problem of this solution is that the language spec itself doesn't guarantee that the fields of an object is ordered as we declare, although most nowadays implementations do.
It would if you had used getOwnPropertyNames. But Object.keys does indeed not have that guarantee (because it predates ES2015, when order was defined).
1

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.

2 Comments

The problem of this solution is that the language spec itself doesn't guarantee that the fields of an object is ordered as we declare, although most nowadays implementations do.
@tibetty: It would if the author had used getOwnPropertyNames. But Object.keys does indeed not have that guarantee (because it predates ES2015, when order was defined).
-1

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.

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.