[英]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? 如何在JavaScript中执行此操作? Or is there a lodash function which can do this? 还是有lodash函数可以做到这一点?
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: 在ES2015 +中,您可以使用解构分配:
[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. 如果您要按其位置将array
的条目分配给obj
的对象属性, 请注意,对象属性顺序自ES2015起是新的,容易被误解。
But with a fully ES2015+ compliant JavaScript engine, if your object really is defined as: 但是如果您的对象确实定义为:具有完全符合ES2015 +的JavaScript引擎, 则 :
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: ...然后, Object.getOwnPropertyNames
将可靠地返回["first-name", "last-name"]
( 不保证Object.keys
会返回),因为:
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. 再次:这仅适用于完全符合ES2015 +的JavaScript引擎,并具有如上所示定义的对象。 The order in which properties are reported by getOwnPropertyNames
is potentially complex; getOwnPropertyNames
报告属性的顺序可能很复杂。 details in the spec . 规范中的细节 。
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. 除了Object.keys(obj)
部分,其他所有内容都是不言自明的。 It just returns an array of the keys of obj(`["first-name", "last-name"]) it gets each one and assigns them to you array. 它只是返回obj(`[“ first-name”,“ last-name”])键的数组,它获取每个键并将它们分配给您的数组。
EDIT: 编辑:
Also, as tibetty said in the comment, you need an order array. 另外,正如评论中所说的,您需要一个订单数组。
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;
}
Javascript has no concept of associative array like PHP. Javascript没有像PHP这样的关联数组的概念。 You can create JSON (Javascript Object Notation). 您可以创建JSON(JavaScript对象表示法)。 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. JavaScript具有类的概念。 You can create class having member first name , last name. 您可以创建具有成员名字,姓氏的类。 Instance it and assign your values. 实例化它并分配您的值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.