Subpar Web Dev
Subpar Web Dev

Reputation: 3280

What is the best way in JavaScript to trim down the properties of an object?

Situation:

I have an object like

{ prop_1 : val_1, prop_2 : val_2, prop_3 : val_3 , ..., prop_N : val_N } 

and I want to remove all properties that aren't prop_i, prop_j or prop_K ?

What is the best way to do this other than the "brute force" way of

var original = { prop_1 : val_1, prop_2 : val_2, prop_3 : val_3 , ..., prop_N : val_N };
var newguy = { prop_i : original.prop_i, prop_j : original.prop_j, prop_k : original.prop_k };
original = newguy;

????

Upvotes: 0

Views: 109

Answers (4)

user663031
user663031

Reputation:

In ES6, you can write

({prop_i, prop_j, prop_K}) => ({prop_i, prop_j, prop_K})(original)

This works by defining a function which deconstructs its arguments into certain property values, and returns an object with those values. Then call the function on the input object.

See One-liner to take some properties from object in ES 6.

Upvotes: 0

Paul S.
Paul S.

Reputation: 66364

Simple .forEach and .indexOf over Object.keys to delete non-matches

function cleanExcept(o, whitelist) {
    Object.keys(o).forEach(k => whitelist.indexOf(k) !== -1 ? 0 : delete o[k]);
    return o;
}


var o = {foo: 'foo', bar: 'bar', fizz: 'fizz', buzz: 'buzz'};
cleanExcept(o, ['foo', 'fizz']); // Object {foo: "foo", fizz: "fizz"}

Using an Object cache instead of .indexOf, as per @dandavis

function cleanExcept(o, whitelist) {
    var w = {};
    whitelist.forEach(k => w[k] = true);
    Object.keys(o).forEach(k => w[k] ? 0 : delete o[k]);
    return o;
}

Modifying this cache just take the values you want and return it (i.e. you get a new object reference)

function cleanExcept(o, whitelist) {
    var w = {};
    whitelist.forEach(k => !(k in o) ? 0 : w[k] = o[k]);
    return w;
}

Upvotes: 0

rabbit.aaron
rabbit.aaron

Reputation: 2597

Well you can do a function to help you do that.

(function() {
  'use strict';

  function copyOnly(obj, keysToPreserve) {
    var result = {};
    for (var i = 0, length = keysToPreserve.length; i < length; ++i) {
      var key = keysToPreserve[i];
      result[key] = obj[key];
    }
    return result;
  }

  function copyExclude(obj, keysToExclude) {
    var result = {};
    for (var key in obj) {
      if (obj.hasOwnProperty(key) && keysToExclude.indexOf(key) === -1) { // -1 means key doesn't exist in keysToExclude
        result[key] = obj[key];
      }
    }
    return result;
  }

  var original = {
    a: '1',
    b: '2',
    c: '3',
    d: '4',
    e: '5'
  };

  var toPreserve = ['a', 'b', 'c'];
  var result1 = copyOnly(original, toPreserve);
  
  var toExclude = ['d', 'e'];
  var result2 = copyExclude(original, toExclude);
  
  // result1 will have the same structure as result2
  
  document.getElementById('result').innerHTML = 'result1 = ' + JSON.stringify(result1) + '\n' + 'result2 = ' + JSON.stringify(result2);
})();
<pre id="result"></pre>

Upvotes: 1

code_monk
code_monk

Reputation: 10128

Here is a non-brute-force way. It uses a whitelist, iterates over them, and copies values from "oldguy".

var oldguy = {
  "prop_1": 1,
  "prop_2": 2,
  "prop_3": 3,
  "prop_i": "i",
  "prop_j": "j",
  "prop_k": "k",
  "prop_N": "N",
  "prop_z": "Z"
};

var newguy = {};

var keys_to_include = ['prop_i', 'prop_j', 'prop_k'];

keys_to_include.forEach(function(k){
    newguy[k] = oldguy[k];
});

$('#output').html( JSON.stringify(newguy,null,'  ') );
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<pre><code id="output"></code></pre>

Upvotes: 0

Related Questions