529

Let's say I have an object:

{
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
}

I want to create another object by filtering the object above so I have something like.

 {
    item1: { key: 'sdfd', value:'sdfd' },
    item3: { key: 'sdfd', value:'sdfd' }
 }

I am looking for a clean way to accomplish this using Es6, so spread operators are available to me.

4
  • ES6 has no object spread operators, and you don't need them here anyway Commented Aug 3, 2016 at 18:32
  • 2
    Possible duplicate of JavaScript: filter() for Objects Commented Mar 5, 2017 at 9:45
  • @DanDascalescu But this answer gives an ES6 way of accomplishing what the OP asks, doesn't it? Commented May 12, 2017 at 11:27
  • 2
    What if I wanted to filter by a key/value? Commented Apr 3, 2019 at 2:15

31 Answers 31

939
+150

If you have a list of allowed values, you can easily retain them in an object using:

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = Object.keys(raw)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    obj[key] = raw[key];
    return obj;
  }, {});

console.log(filtered);

This uses:

  1. Object.keys to list all properties in raw (the original data), then
  2. Array.prototype.filter to select keys that are present in the allowed list, using
    1. Array.prototype.includes to make sure they are present
  3. Array.prototype.reduce to build a new object with only the allowed properties.

This will make a shallow copy with the allowed properties (but won't copy the properties themselves).

You can also use the object spread operator to create a series of objects without mutating them (thanks to rjerue for mentioning this):

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = Object.keys(raw)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    return {
      ...obj,
      [key]: raw[key]
    };
  }, {});

console.log(filtered);

For purposes of trivia, if you wanted to remove the unwanted fields from the original data (which I would not recommend doing, since it involves some ugly mutations), you could invert the includes check like so:

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

Object.keys(raw)
  .filter(key => !allowed.includes(key))
  .forEach(key => delete raw[key]);

console.log(raw);

I'm including this example to show a mutation-based solution, but I don't suggest using it.

Sign up to request clarification or add additional context in comments.

13 Comments

Thanks that worked great. I also found an approach by using 'deconstruction syntax. IE : const {item1,item3} = raw const newObject = {item1,item3}
Deconstruction will work (just fine), but is purely compile-time. You can't make it a dynamic list of properties or provide any complex rules (a loop can have validation callbacks attached, for example).
I can't vote this up enough! Well done for using filter and reduce and not constructing another object from a for loop. And awesome that you explicitly separated the immutable and mutable versions. +1
Quick warning: Array.prototype.includes is not part of ES6. It's introduced in ECMAScript 2016 (ES7).
If you want to do the reduce in an immutable way, you could also replace the function contents with return { ...obj, [key]: raw[key] }
|
198

If you are OK with using ES6 syntax, I find that the cleanest way to do this, as noted here and here is:

const data = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const { item2, ...newData } = data;

Now, newData contains:

{
  item1: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

Or, if you have the key stored as a string:

const key = 'item2';
const { [key]: _, ...newData } = data;

In the latter case, [key] is converted to item2 but since you are using a const assignment, you need to specify a name for the assignment. _ represents a throw away value.

More generally:

const { item2, ...newData } = data; // Assign item2 to item2
const { item2: someVarName, ...newData } = data; // Assign item2 to someVarName
const { item2: _, ...newData } = data; // Assign item2 to _
const { ['item2']: _, ...newData } = data; // Convert string to key first, ...

Not only does this reduce your operation to a one-liner but it also doesn't require you to know what the other keys are (those that you want to preserve).

A simple utility function would look like this:

function removePropFromObject(obj, prop) {
  const { [prop]: _, ...rest } = obj
  return { ...rest }
}

6 Comments

" _ represents a throw away value" where does this come? First time I see it
I believe this is an adopted convention by the JS community. An _ is simply a valid variable name that can be used in JS but since it is pretty much nameless, it really shouldn't be used in that way if you care to convey intent. So, it's been adopted as a way to denote a variable you don't care about. Here's further discussion about it: stackoverflow.com/questions/11406823/…
This is much tidier than the accepted answer, avoids the overhead of creating a new array with Object.keys(), and the overhead of iterating the array with filter and reduce.
@yhabib the _ doesn't matter, it's just a variable name, you can rename it to anything you want
In the removePropFromObject function, is there any need to return the value spreaded again? I.e. couldn't you just do return rest instead of return { ...rest }?
|
94

You can now make it shorter and simpler by using the Object.fromEntries method (check browser support):

const raw = { item1: { prop:'1' }, item2: { prop:'2' }, item3: { prop:'3' } };

const allowed = ['item1', 'item3'];

const filtered = Object.fromEntries(
   Object.entries(raw).filter(
      ([key, val])=>allowed.includes(key)
   )
);

read more about: Object.fromEntries

5 Comments

This is now the best way I think. Much more intuitive than reduce.
For the record. When using Object.fromEntries with Typescript make sure that "lib": ["ES2019"] is set in tsconfig.json
I like the entries/fromEntries trick but if it's a big object, it might hurt performance - jsbench.me/3ukxxhfgjr/1
I'm interested in how this cases are handled by JS Engine (V8 for example)? In my understanding of inline caches example with loop will give us a megamorphic object and approach with object from entries should construct object with single shape
Here a type-safe (TypeScript) version of the code in the answer: const excludeKeysFromObject = <T extends {}>(obj: T, allowedKeys: (keyof T)[]) => Object.fromEntries((Object.keys(obj) as (keyof T)[]).filter(key => allowedKeys.includes(key)).map(key => [key, obj[key]]))
74

Nothing that hasn't been said before, but to combine some answers to a general ES6 answer:

const raw = {
  item1: { key: 'sdfd', value: 'sdfd' },
  item2: { key: 'sdfd', value: 'sdfd' },
  item3: { key: 'sdfd', value: 'sdfd' }
};

const filteredKeys = ['item1', 'item3'];

const filtered = filteredKeys
  .reduce((obj, key) => ({ ...obj, [key]: raw[key] }), {});

console.log(filtered);

2 Comments

this is a much more simple and performant solution than others ;)
Good succinct solution. Note that if there's a key in filteredKeys that's not in raw, that key will show up in filtered with a value of undefined. In other answers, that key won't show up in filtered at all.
67

The cleanest way you can find is with Lodash#pick

const _ = require('lodash');

const allowed = ['item1', 'item3'];

const obj = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
}

const filteredObj = _.pick(obj, allowed)

3 Comments

It's important to point out that for it an extra project dependency has to be downloaded on runtime.
_.omit(obj, ["item2"]) - lodash.omit is an opposite to lodash.pick
There's also ramda.pick if you use Ramda! ramdajs.com/docs/#pick
36

Just another solution in one line of Modern JS with no external libraries.

I was playing with "Destructuring" feature :

const raw = {
    item1: { key: 'sdfd', value: 'sdfd' },
    item2: { key: 'sdfd', value: 'sdfd' },
    item3: { key: 'sdfd', value: 'sdfd' }
  };
var myNewRaw = (({ item1, item3}) => ({ item1, item3 }))(raw);
console.log(myNewRaw);

12 Comments

var myNewRaw = (({ item1, item3}) => ({ item1, item3 }))(raw); Syntax issue
There are a couple of things condensed here: First thing first, there is the declaration of the function. it is an arrow function (it takes an object and return an object) . It is the same thing as function(obj){return obj;}. the second thing is that ES6 allow destructuring future. In my function declaration I destructure my object{ item1, item3}. And the last thing is that I self invoke my function. You can use self invoke function to manage your scope for example. But here it was just to condense the code. I hope it is clear. If I miss something feel free to add more.
this is the preferred modern approach imho
The accepted answer will only include keys that are in the filter list AND exist in the original data. I believe your answer will insert missing keys with undefined as the value, for example if my original data didn't include item3, I think your code would add it. There might be use-cases where that's undesirable. (for my use-case it's fine! I'm using it!)
@NikaKasradze without those parentheses the function will return undefined. Awol did help me with that point at the first time. And after reading blogs and read the specification I understand it better. It will try a brief explanation in another comment if you do not mind.
|
36

Building on these two answers:

https://stackoverflow.com/a/56081419/13819049
https://stackoverflow.com/a/54976713/13819049

We can do:

const original = { a: 1, b: 2, c: 3 };
const allowed = ['a', 'b'];

const filtered = Object.fromEntries(allowed.map(k => [k, original[k]]));

Which is cleaner and faster:

https://jsbench.me/swkv2cbgkd/1

3 Comments

Beauty one-liner; just what I needed, thx.
Holy smokes, I have no idea what this is doing but it sure saved me!
It goes through each item in allowed, and converts it to a tuple (fancy way of saying array) of two things - the first being the item, the second being the value of 'original'. Then we have an array of 'entries' [['a', 1], ['b', 2]] and they get converted into an object
23

ok, how about this one-liner

    const raw = {
      item1: { key: 'sdfd', value: 'sdfd' },
      item2: { key: 'sdfd', value: 'sdfd' },
      item3: { key: 'sdfd', value: 'sdfd' }
    };

    const filteredKeys = ['item1', 'item3'];

    const filtered = Object.assign({}, ...filteredKeys.map(key=> ({[key]:raw[key]})));

1 Comment

what if you only know the keys you want to remove...not that keys you want to keep?
22

Another solution using the Array.reduce method on the allowed keys:

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = allowed.reduce((obj, key) => { 
  obj[key] = raw[key]; 
  return obj 
}, {})

console.log(filtered);

Especially in case of larger source objects (raw in this example) it would make sense. The iteration will not be executed using all the entries of the source, but only using the keys you want to filter, thus shorter/faster...

Demonstration in this Fiddle...


But I do have to say I also like the solution in this answer here which is using Object.fromEntries Array.filter and Array.includes:

const object = Object.fromEntries(
  Object.entries(raw).filter(([key, value]) => allowed.includes(key))
);

Demonstration in this Fiddle...

4 Comments

Wilt, your second approach is an exact duplicate of this answer provided last year: stackoverflow.com/a/56081419/5522000
@ArtSchmidt Thank you for your comment: Missed that one in the long list. I will refer to that answer instead.
What is "new" about .reduce()? I suppose this comment comes from the future but I don't think it was new in 2020, either.
@VLAZ, not sure why I wrote that at the time, maybe because some people even today still want to support IE8 and it won't work there. I removed the "new" from the answer...
13

You can add a generic ofilter (implemented with generic oreduce) so you can easily filter objects the same way you can arrays –

const oreduce = (f, acc, o) =>
  Object
    .entries (o)
    .reduce
      ( (acc, [ k, v ]) => f (acc, v, k, o)
      , acc
      )

const ofilter = (f, o) =>
  oreduce
    ( (acc, v, k, o)=>
        f (v, k, o)
          ? Object.assign (acc, {[k]: v})
          : acc
    , {}
    , o
    )

We can see it working here -

const data =
  { item1: { key: 'a', value: 1 }
  , item2: { key: 'b', value: 2 }
  , item3: { key: 'c', value: 3 }
  }

console.log
  ( ofilter
      ( (v, k) => k !== 'item2'
      , data
      )
      // [ { item1: { key: 'a', value: 1 } }
      // , { item3: { key: 'c', value: 3 } }
      // ]

  , ofilter
      ( x => x.value === 3
      , data
      )
      // [ { item3: { key: 'c', value: 3 } } ]
  )

Verify the results in your own browser below –

const oreduce = (f, acc, o) =>
  Object
    .entries (o)
    .reduce
      ( (acc, [ k, v ]) => f (acc, v, k, o)
      , acc
      )

const ofilter = (f, o) =>
  oreduce
    ( (acc, v, k, o)=>
        f (v, k, o)
          ? Object.assign (acc, { [k]: v })
          : acc
    , {}
    , o
    )

const data =
  { item1: { key: 'a', value: 1 }
  , item2: { key: 'b', value: 2 }
  , item3: { key: 'c', value: 3 }
  }

console.log
  ( ofilter
      ( (v, k) => k !== 'item2'
      , data
      )
      // [ { item1: { key: 'a', value: 1 } }
      // , { item3: { key: 'c', value: 3 } }
      // ]

  , ofilter
      ( x => x.value === 3
      , data
      )
      // [ { item3: { key: 'c', value: 3 } } ]
  )

These two functions could be implemented in many ways. I chose to attach to Array.prototype.reduce inside oreduce but you could just as easily write it all from scratch

5 Comments

I like your solution, but I don't know how much clearer / more efficient it is compared to this one.
Here is a benchmark showing that your solution is the fastest.
In oreduce, the first acc is shadowed in .reduce(acc, k), and in ofilter the o is shadowed - oreduce is called with a variable called o as well - which is which?
in ofilter the variable o is shadowed but it always points to the the same input var; that’s why its shadowed here, because it’s the same - the accumulator (acc) is also shadowed because it more clearly shows how the data moves thru the lambda; acc is not always the same binding, but it always represents the current persistent state of the computational result we wish to return
While the code works well and the method is very good I do wonder what help writing code like that is to somebody obviously needing help with javascript. I'm all for brevity but that is almost as compact as a minimized code.
10

The answers here are definitely suitable but they are a bit slow because they require looping through the whitelist for every property in the object. The solution below is much quicker for large datasets because it only loops through the whitelist once:

const data = {
  allowed1: 'blah',
  allowed2: 'blah blah',
  notAllowed: 'woah',
  superSensitiveInfo: 'whooooah',
  allowed3: 'bleh'
};

const whitelist = ['allowed1', 'allowed2', 'allowed3'];

function sanitize(data, whitelist) {
  return whitelist.reduce(
    (result, key) =>
      data[key] !== undefined
        ? Object.assign(result, { [key]: data[key] })
        : result,
    {}
  );
}

const result = sanitize(data, whitelist);

console.log(result);

Comments

9

This is how I did it, recently:

const dummyObj = Object.assign({}, obj);
delete dummyObj[key];
const target = Object.assign({}, {...dummyObj});

2 Comments

Hm. It seems you're mixing old and new syntax. Object.assign == ... You could write const dummyObj = { ...obj } and const target = { ...dummyObj }. Plus, the latter is not at all necessary, since you could directly work with dummyObj afterwards.
This does three copies of an object. Just to only get one of those with the key removed: dummyObj = Object.assign({}, obj) - clone obj into a new object called dummyObj; delete dummyObj[key]; - remove the key. At this point your job is done. But then: {...dummyObj} - clone dummyObj (which is already a clone) into a temporary object; ` target = Object.assign({}, {...dummyObj})` - clone the cloned temporary object (a clone of a clone) into a new object called target.
8

A simpler solution without using filter can be achieved with Object.entries() instead of Object.keys()

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = Object.entries(raw).reduce((acc,elm)=>{
  const [k,v] = elm
  if (allowed.includes(k)) {
    acc[k] = v 
  }
  return acc
},{})

Comments

7

Piggybacking on ssube's answer.

Here's a reusable version.

Object.filterByKey = function (obj, predicate) {
  return Object.keys(obj)
    .filter(key => predicate(key))
    .reduce((out, key) => {
      out[key] = obj[key];
      return out;
    }, {});
}

To call it use

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

var filtered = Object.filterByKey(raw, key => 
  return allowed.includes(key));
});

console.log(filtered);

The beautiful thing about ES6 arrow functions is that you don't have to pass in allowed as a parameter.

Comments

6

Simple Way! To do this.

const myData = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};
const{item1,item3}=myData
const result =({item1,item3})

6 Comments

You are not actually filtering anything here, just destructuring the data given. But what happens when the data changes?
@IdrisDopicoPeña - The destructuring is the filter. How is an object of fieldnames any different to using an array of field names? (though this solution will add missing keys)
@charles-allen an array of field names can be changed more easily without requiring the code that uses it to change. So you can have a reusable function that takes that array and filters your object, e.g., myFunc(["item1", "item3"]) can later be reused as myFunc(["foo", "bar"]) without changing anything about how it works. With this approach you need different function for each object you want to filter the keys of.
@VLAZ - True. Maybe I misunderstood Idris. I thought they meant different myData, not a different filter (that is my use case -- I know what properties I need at compile time, so this solution is much sleaker).
@charles-allen I'm not saying this solution is wrong. It's indeed very useful. However, the question was what's the difference. If I needed to extract item1 and item3 one time and then foo and bar another time, I'd probably just go with destructuring twice. If I need to do it a lot more often than that or I don't even know which ones I want, then I'd go with a function.
|
5

You can do something like this:

const base = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const filtered = (
    source => { 
        with(source){ 
            return {item1, item3} 
        } 
    }
)(base);

// one line
const filtered = (source => { with(source){ return {item1, item3} } })(base);

This works but is not very clear, plus the with statement is not recommended (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/with).

1 Comment

This...is horrible. I'm aware with has its (very, very few) uses but this isn't one of them. It's just destructuring with extra steps. source => { with(source){ return {item1, item3} } } is equivalent to ({item1, item3}) => ({item1, item3}) which is already an answer.
5
const filteredObject = Object.fromEntries(Object.entries(originalObject).filter(([key, value]) => key !== uuid))

1 Comment

There are other answers that provide the OP's question, and they were posted some time ago. When posting an answer, please make sure you add either a new solution, or a substantially better explanation, especially when answering older questions.
5

I know that this already has plenty of answers and is a rather old question. But I just came up with this neat one-liner:

JSON.parse(JSON.stringify(raw, ['key', 'value', 'item1', 'item3']))

That returns another object with just the whitelisted attributes. Note that the key and value is included in the list.

2 Comments

Love this! Probably really quick execution, and it's easy to filter out one or more properties
@JanisJansen serialising and de-serialising an object doesn't seem very quick to me. Also, you'd lose values like functions, or undefined, or NaN or BigInts.
3

There are many ways to accomplish this. The accepted answer uses a Keys-Filter-Reduce approach, which is not the most performant.

Instead, using a for...in loop to loop through keys of an object, or looping through the allowed keys, and then composing a new object is ~50% more performanta.

const obj = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const keys = ['item1', 'item3'];

function keysReduce (obj, keys) {
  return keys.reduce((acc, key) => {
    if(obj[key] !== undefined) {
      acc[key] = obj[key];
    }
    return acc;
  }, {});
};

function forInCompose (obj, keys) {
  const returnObj = {};
  for (const key in obj) {
    if(keys.includes(key)) {
      returnObj[key] = obj[key]
    }
  };
  return returnObj;
};

keysReduce(obj, keys);   // Faster if the list of allowed keys are short
forInCompose(obj, keys); // Faster if the number of object properties are low

a. See jsPerf for the benchmarks of a simple use case. Results will differ based on browsers.

Comments

2

Another short way

function filterByKey(v,keys){
  const newObj ={};
  keys.forEach(key=>{v[key]?newObj[key]=v[key]:''});
  return newObj;
}

//given
let obj ={ foo: "bar", baz: 42,baz2:"blabla" , "spider":"man", monkey:true};

//when
let outObj =filterByKey(obj,["bar","baz2","monkey"]);

//then 
console.log(outObj);
  //{
  //  "baz2": "blabla",
  //  "monkey": true
  //}

Comments

2

You can remove a spesific property on your object

items={
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
}

// Example 1
var key = "item2";
delete items[key]; 

// Example 2
delete items["item2"];

// Example 3
delete items.item2;

Comments

2

The following method takes the object and any properties to filter out.

function removeObjectKeys(obj, ...keysToRemove) {
  let mObject = { ...obj }
  for (let key of keysToRemove) {
    const { [String(key)]: _, ...rest } = mObject
    mObject = { ...rest }
  }
  return mObject
}
    
const obj = { 123: "hello", 345: "world", 567: "and kitty" };
const filtered = removeObjectKeys(obj, 123);
console.log(filtered);

const twoFiltered = removeObjectKeys(obj, 345, 567);
console.log(twoFiltered);

1 Comment

this gives a linting error as the _ is not used anywhere else, but it's very creative none the less.
2

Many of the above solutions repeatedly call Array.prototype.includes for each key in raw, which will make the solution O(n·m) (where n is the number of keys in the object an m is the length of the allowed list).

This could be avoided by using an allowed Set, but iterating over the allowed keys and copying them to an initially-empty object gives very simple, readable code that is O(m):

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = {};
for (const key of allowed) {
  if (key in raw) filtered[key] = raw[key];
}

console.log(filtered);

You could also use raw.hasOwnProperty(key) instead of key in raw if you wanted to avoid copying inherited properties.

Comments

1

OK, how about this:

const myData = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

function filteredObject(obj, filter) {
  if(!Array.isArray(filter)) {
   filter = [filter.toString()];
  }
  const newObj = {};
  for(i in obj) {
    if(!filter.includes(i)) {
      newObj[i] = obj[i];
    }
  }
  return newObj;
}

and call it like this:

filteredObject(myData, ['item2']); //{item1: { key: 'sdfd', value:'sdfd' }, item3: { key: 'sdfd', value:'sdfd' }}

Comments

1

This function will filter an object based on a list of keys, its more efficient than the previous answer as it doesn't have to use Array.filter before calling reduce. so its O(n) as opposed to O(n + filtered)

function filterObjectByKeys (object, keys) {
  return Object.keys(object).reduce((accum, key) => {
    if (keys.includes(key)) {
      return { ...accum, [key]: object[key] }
    } else {
      return accum
    }
  }, {})
}

Comments

1

I'm surprised how nobody has suggested this yet. It's super clean and very explicit about which keys you want to keep.

const unfilteredObj = {a: ..., b:..., c:..., x:..., y:...}

const filterObject = ({a,b,c}) => ({a,b,c})
const filteredObject = filterObject(unfilteredObject)

Or if you want a dirty one liner:

const unfilteredObj = {a: ..., b:..., c:..., x:..., y:...}

const filteredObject = (({a,b,c})=>({a,b,c}))(unfilteredObject);

3 Comments

This method will add a key not present in the original array. Might be a problem or not, this should be at least pointed out.
I think this guy suggested it already: stackoverflow.com/a/49340650/2957169
"I'm surprised how nobody has suggested this yet." it's already been suggested by Novy in November 2018 and by ghanshyam bendkoli in December 2019.
0

During loop, return nothing when certain properties/keys are encountered and continue with the rest:

const loop = product =>
Object.keys(product).map(key => {
    if (key === "_id" || key === "__v") {
        return; 
    }
    return (
        <ul className="list-group">
            <li>
                {product[key]}
                <span>
                    {key}
                </span>
            </li>
        </ul>
    );
});

Comments

0

Another approach would be to use Array.prototype.forEach() as

const raw = {
  item1: {
    key: 'sdfd',
    value: 'sdfd'
  },
  item2: {
    key: 'sdfd',
    value: 'sdfd'
  },
  item3: {
    key: 'sdfd',
    value: 'sdfd'
  }
};

const allowed = ['item1', 'item3', 'lll'];

var finalObj = {};
allowed.forEach(allowedVal => {
  if (raw[allowedVal])
    finalObj[allowedVal] = raw[allowedVal]
})
console.log(finalObj)

It includes values of only those keys which are available in the raw data and thus preventing adding any junk data.

Comments

0

That would be my solution:

const filterObject = (obj, condition) => {
    const filteredObj = {};
    Object.keys(obj).map(key => {
      if (condition(key)) {
        dataFiltered[key] = obj[key];
      }
    });
  return filteredObj;
}

Comments

0

use PropPick package


pick('item1 item3', obj);
// {
//   item1: { key: 'sdfd', value:'sdfd' },
//   item3: { key: 'sdfd', value:'sdfd' }
// }

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.