javascript 向现有 js 对象添加多个属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16427280/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 04:37:40  来源:igfitidea点击:

Add multiple attributes to an existing js object

javascriptoopobject

提问by Don P

I want to add multiple attributes to an existing object with existing attributes. Is there a more concise way than one line per new attribute?

我想将多个属性添加到具有现有属性的现有对象。有没有比每个新属性一行更简洁的方法?

myObject.name = 'don';
myObject.gender = 'male';

Everything on MDN shows how to do new objects with bracket notation, but not existing objects: https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects

MDN 上的所有内容都展示了如何使用括号表示法创建新对象,而不是现有对象:https: //developer.mozilla.org/en-US/docs/JavaScript/Guide/Working_with_Objects

采纳答案by Steven Wexler

From How can I merge properties of two JavaScript objects dynamically?

如何动态合并两个 JavaScript 对象的属性?

var obj2 = {name: 'don', gender: 'male'};
for (var attrname in myobject) { myobject[attrname] = obj2[attrname]; }

EDIT

编辑

To be a bit clearer about how you could extend Object to use this function:

更清楚地了解如何扩展 Object 以使用此函数:

//Extend the protype so you can make calls on the instance
Object.prototype.merge = function(obj2) {
    for (var attrname in obj2) {
        this[attrname] = obj2[attrname];
    }
    //Returning this is optional and certainly up to your implementation.  
    //It allows for nice method chaining.
    return this;
};
//Append to the object constructor function so you can only make static calls
Object.merge2 = function(obj1, obj2) {
    for (var attrname in obj2) {
        obj1[attrname] = obj2[attrname];
    }
    //Returning obj1 is optional and certainly up to your implementation
    return obj1;
};

Usage:

用法:

var myObject1 = { One: "One" };
myObject1.merge({ Two: "Two" }).merge({ Three: "Three" });
//myObject1 is { One: "One", Two: "Two", Three: "Three", merge: function }


var myObject2 = Object.merge2({ One: "One" }, { Two: "Two" });
Object.merge2(myObject2, { Three: "Three" });
//myObject2 is { One: "One", Two: "Two", Three: "Three" }

Note: You certainly could implement a flexible merge conflict strategy depending on your needs.

注意:您当然可以根据需要实施灵活的合并冲突策略。

回答by Ed Barahona

In ES6/ ES2015 you can use the Object.assign method

在 ES6/ES2015 中你可以使用 Object.assign 方法

let obj = {key1: true};
console.log('old obj: ', obj);
let newObj = {key2: false, key3: false};

Object.assign(obj, newObj);
console.log('modified obj: ', obj);

回答by letiagoalves

Sometimes I do it like this:

有时我是这样做的:

var props = {
   name : 'don',
   gender : 'male',
   age : 12,
   other : false
};
for(var p in props) myObject[p] = props[p];

回答by Shay Yzhakov

I'm not sure if that's helpful, but there is a trick using JS ES6 spread syntax:

我不确定这是否有帮助,但有一个使用 JS ES6 扩展语法的技巧:

let obj = {a: 1, b: 2};
obj = {...obj, b: 3, c: 4};
console.log(obj); // {a: 1, b: 3, c: 4}

You can try to use this somehow... In general, that's a single liner to add or override object properties using js destructuring method.

您可以尝试以某种方式使用它... 一般来说,这是使用 js 解构方法添加或覆盖对象属性的单个衬垫。

回答by JimmyRare

In ECMAscript 5 you can make us of defineProperties:

在 ECMAscript 5 中,您可以让我们使用defineProperties

Object.defineProperties(myobject, {  
  name: {  
    value: 'don' 
  },  
  gender: {  
    value: 'male' 
  }  
});

回答by zavg

Use jQuery library

使用 jQuery 库

jQuery.extend(myObject, { name : 'don', gender : 'male' });