简体   繁体   English

如何在JavaScript中将数组元素指定为对象中的属性值?

[英]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会返回),因为:

  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. 再次:这仅适用于完全符合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.

相关问题 Javascript:如何将数组值添加到对象属性 - Javascript: How to add array values to object property 如何将对象的属性值提取到数组中并使数组中的元素不同? - How to extract the property values of a object into an array and make elements in the array different? 如果Object属性具有某些定界符,如何分割JavaScript数组元素 - How to Split JavaScript Array Elements if an Object property has certain delimitter 如何使用 javascript 中的数组值更改数组 object 中的属性值 - How to change property value in array object with array values in javascript 如何将数组 Object 中的属性值替换为 Javascript 中另一个数组 Object 中的属性值 - How to Replace values of a property in an Array Object with values of a property in another Array Object in Javascript 获取Java数组元素的特定属性的值 - Get values of specific property of array elements in Javascript 在javascript中,如何将数组的元素替换为另一个对象中的对应值? - In javascript how to replace elements of an array to it's corresponding values in another object? 如何使用JavaScript在嵌套对象数组中按属性分配值 - How to assign values by property in nested object array using javascript 如何将 JavaScript object 的属性值提取到数组中? - How might I extract the property values of a JavaScript object into an array? 如何检查两个数组中的属性值 object 值 javascript - how to check property value in two array of object values javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM