如何将公共属性添加到所有Object的属性(简写)

时间:2018-02-07 14:13:48

标签: javascript

我的Object个属性中的每一个都有公共属性(称为:selected),是否有一种简短的方法将此属性注入每个属性而不显式写入每个属性?

请注意,我已经查看了这个answer,但它只讨论arrays

var myObject = {
    propery1: {
        selected: function () {
            return this.value === someVariable;
        },                    
        value: 1,                    
    },
    propery2: {
        selected: function () {
            return this.value === someVariable;
        },                    
        value: 2,                    
    },
    propery3: {
        selected: function () {
            return this.value === someVariable;
        },                    
        value: 3,                    
    },

    //...

    propery10: {
        selected: function () {
            return this.value === someVariable;
        },
        value: 10,
    },
};

例如:

var someVariable = 1;

输出:

myObject = {
    propery1: {
        selected: true,                    
        value: 1,                    
    },
    propery2: {
        selected: false,
        value: 2,                    
    },
    propery3: {
        selected: false,
        value: 3,                    
    },

    //...

    propery10: {
        selected: false,
        value: 10,
    },
};

5 个答案:

答案 0 :(得分:1)

您可以创建对象的每个键的映射,然后根据唯一条件为每个键设置特定属性。

var myObj = {prop1: {...}, prop2: {...}};
Object.keys(myObj).map(k => k).forEach(k => myObj[k].selected = ...);

答案 1 :(得分:1)

一种解决方案是实现某种混合。它看起来像这样:

var someVariable = 1

var selected = {
  selected: function() {
    return this.value === someVariable;
  }
}

var myObject = {
  property1: Object.assign({
    value: 1,
  }, selected),
  property2: Object.assign({
    value: 2,
  }, selected),
};

console.log(myObject.property1.selected(), myObject.property2.selected())

使用ES2015 spread syntax可以看起来更好:

var someVariable = 1

var selected = {
  selected: function() {
    return this.value === someVariable;
  }
}

var myObject = {
  property1: {
    ...selected,
    value: 1,
  },
  property2: {
    ...selected,
    value: 2,
  },
};

console.log(myObject.property1.selected(), myObject.property2.selected())

答案 2 :(得分:1)

已经有一些答案描述了如何做这些事情。另一种方法是您也可以使用Object.create()来执行此操作.Object.create在__ proto __中添加所选属性。因此,要在 for..in 循环中访问它,您必须使用属性描述符将 enumerable 属性设置为 {{1} }

true

答案 3 :(得分:1)

你可以绑定内部对象,调用函数并赋值。

var object = { propery1: { selected: function () { return this.value === someVariable; }, value: 1 }, propery2: { selected: function () { return this.value === someVariable; }, value: 2 }, propery3: { selected: function () { return this.value === someVariable; }, value: 3 }, propery10: { selected: function () { return this.value === someVariable; }, value: 10 } },
    someVariable = 1;

Object.keys(object).forEach(k => object[k].selected = object[k].selected.call(object[k]));

console.log(object);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 4 :(得分:1)

编写可重用的函数可能会为将来的目的派上用场!

function addKeys(object, properties) {
    var key,        // To loop the object
        index,      // To loop the properties array
        property,   // To store each property
        subObject;  // To store each object

    // Loop through the object
    for(key in object) {
        // Store the current object for easy access
        subObject = object[key];
        // Loop through the properties array
        for(index = 0; index < properties.length; index++) {
            // Store the current property for easy access
            property = properties[index];
            // Assign the new property to the current object
            subObject[property.key] = property.value;
        }
    }
}

var someVariable = 1;
var myObject = {
    property1: {
        value: 1,                    
    },
    property2: {
        value: 2,                    
    },
    property3: {
        value: 3,                    
    },
    property10: {
        value: 10,
    },
};

var keysToBeAdded = [{
    key: 'selected',
    value: function() {
        return this.value === someVariable;
    }
}, {
    key: 'someOtherKey',
    value: 'someOtherValue'
}];

// Add new keys to your object
addKeys(myObject, keysToBeAdded);

console.log(myObject);

我使用了基本循环来使函数执行得更快。您可以使用Object.keys获取所有对象密钥并对其进行处理,并使用array.forEach代替for循环。