简体   繁体   English

从对象中删除额外的属性

[英]Remove extra properties from object

What would be the best way to remove any additional properties from an object that is not defined in defaults object? 从默认对象中未定义的对象中删除任何其他属性的最佳方法是什么?

var 

defaults = {

    color : 'blue',
    size: 9,
    price : 40.00,
    instock : true

},

newItem = {

    color: 'red',
    size : 4,
    price : 20.00
    extra : invalid // discard this
    extra1 : invalid // discard this

},

item = $.extend( defaults, newObject ) ;

Desired output.... 所需的输出...

{
    color : 'red',
    size: 4,
    price : 20.00,
    instock : true

}

Before you call $.extend, put the following. 在调用$ .extend之前,请输入以下内容。

for(variable in newItem) {
    if(!(variable in defaults)) {
        delete newItem[variable];
    }
}

This will loop over every key in newItem and check that it is also a key in defaults . 这将遍历newItem每个键,并检查它是否也是defaults的键。 Note that this will modify newItem , so if that is not desired, you'll need to do some tweaking. 请注意,这将修改newItem ,因此,如果不需要这样做,则需要进行一些调整。

You could reduce Object.keys(defaults) to an object containing either the override value or the default value: 您可以将Object.keys(defaults)简化为包含替代值或默认值的对象:

 var defaults = { color : 'blue', size: 9, price : 40.00, instock : true }, newItem = { color: 'red', size : 4, price : 20.00, extra : 'invalid', extra1 : 'invalid' }; function getOverrides(defaults, obj) { return Object.keys(defaults).reduce(function(result, cur) { result[cur] = cur in obj ? obj[cur] : defaults[cur]; return result; }, {}); } console.log(getOverrides(defaults, newItem)); 

According to this performance comparison: https://jsperf.com/dictionary-contains-key 根据此性能比较: https : //jsperf.com/dictionary-contains-key

The most efficient way to do this is: 最有效的方法是:

for(attr in newItem) {
  if(defaults[attr] === undefined)
    delete newItem[attr];
}

Only merge properties which existing in defaults object: (simple and supports old browsers) 仅合并defaults对象中现有的属性:( 简单并支持旧的浏览器)

 var defaults = { color : 'blue', size: 9, price : 40.00, instock : true }; var newItem = { color: 'red', size : 4, price : 20.00, extra : 'invalid', // discard this extra1 : 'invalid' // discard this }; var result = {}; for (var i in defaults) { result[i] = newItem.hasOwnProperty(i) ? newItem[i] : defaults[i]; } console.log(result); 

Some code that I have been playing with that may be of interest, and an example of how to use it with your question. 我一直在玩的一些代码可能会令人感兴趣,并提供一个如何在您的问题中使用它的示例。

 'use strict'; var slice = Function.call.bind(Array.prototype.slice); var reflectAssign = function assign(target) { return slice(arguments, 1).every(function(source) { if (source == null) { return true; } var object = Object(source); return Reflect.ownKeys(object).every(function(key) { return Reflect.set(target, key, object[key]); }); }); }; var reflectAssignHas = function(target) { var targetKeys = Reflect.ownKeys(target); return slice(arguments, 1).every(function(source) { if (source == null) { return true; } var object = Object(source); return targetKeys.every(function(key) { return Reflect.has(object, key) ? Reflect.set(target, key, object[key]) : true }); }); }; var defaults = { color: 'blue', size: 9, price: 40.00, instock: true }; var newItem = { color: 'red', size: 4, price: 20.00, extra: 'invalid', // discard this extra1: 'invalid' // discard this }; var item = {}; console.log(reflectAssign(item, defaults)); console.log(reflectAssignHas(item, newItem)); console.log(item); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-shim.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/4.5.9/es5-sham.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/es6-shim/0.35.3/es6-shim.js"></script> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM