Keeping only certain properties in a JavaScript object

后端 未结 11 968
时光说笑
时光说笑 2020-12-20 11:05

I have an object. I would like to modify the object (not clone it) by removing all properties except for certain specific properties. For instance, if I started with this o

相关标签:
11条回答
  • 2020-12-20 11:40

    Pass a map of whitelisted keys into an IIFE (immediately invoked function expression); not just elegant but also flexible IMO (especially if moved off into a function not unlike in Juan Mendes' answer)

    var myObj={
        p1:123,
        p2:321,
        p3:{p3_1:1231,p3_2:342},
        p4:'23423',
        //....
        p99:{p99_1:'sadf',p99_2:234},
        p100:3434
    }
    
    var myObj = (function(origObj, whiteListMap) {
        for (var prop in origObj) {
            if (!whiteListMap[prop]) {
                delete origObj[prop];
            }
        }
        return myObj;
    })(myObj, {'p1': 1, 'p2': 1, 'p100': 1});
    
    console.log(JSON.stringify(myObj)); //{"p1":123,"p2":321,"p100":3434}
    
    0 讨论(0)
  • 2020-12-20 11:41

    This was the first hit when googling 'js keep only certain keys' so might be worth an update.

    The most 'elegant' solution might just be using underscore.js

    _.pick(myObj, 'p1', 'p2', 'p100')
    
    0 讨论(0)
  • 2020-12-20 11:53

    Just re-initialise the object:

    myObj = {
        p1:   myObj.p1,
        p2:   myObj.p2,
        p100: myObj.p100
    };
    

    Another way is to delete certain properties, which is less effective:

    var prop = ['p1', 'p2', 'p100'];
    for (var k in myObj) {
        if (prop.indexOf(k) < 0) {
            delete myObj[k];
        }
    }
    
    0 讨论(0)
  • 2020-12-20 11:55

    I suppose you could add a new method to the prototype:

    if (!('keepOnlyTheseProps' in Object.prototype)) {
      Object.prototype.keepOnlyTheseProps = function (arr) {
        var keys = Object.keys(this);
        for (var i = 0, l = keys.length; i < l; i++) {
          if (arr.indexOf(keys[i]) < 0) delete this[keys[i]];
        }
      }
    }
    
    myObj.keepOnlyTheseProps(['p1', 'p2', 'p100']);
    

    Fiddle.

    0 讨论(0)
  • 2020-12-20 11:55

    I Made this short solution for case where I have an array with objects.

    so consider the array below?

    arr=[{"a1":"A1","b1":"B1"},{"a1":"Z1","b1":"X1"}];
    console.log(arr);

    I want to keep only "b1" properties of all objects.

    You can use map() and delete for that as follows:

    arr=[{"a1":"A1","b1":"B1"},{"a1":"Z1","b1":"X1"}];
    arr=arr.map(function(d){
            delete d["a1"];
            return d;
    
        });
    console.log(arr);

    result is an array with objects but only "b1" properties.

    0 讨论(0)
  • 2020-12-20 11:58
    var myObj = {a: 1, b: 2, c:3};
    
    function keepProps(obj, keep) {
        for (var prop in obj) {
            if (keep.indexOf( prop ) == -1) {
                delete obj[prop];
            }             
        }
    }
    
    keepProps(myObj, ['a', 'b']);
    console.log(myObj);
    

    http://jsfiddle.net/mendesjuan/d8Sp3/2/

    0 讨论(0)
提交回复
热议问题