sindresorhus / filter-obj Goto Github PK
View Code? Open in Web Editor NEWFilter object keys and values into a new object
License: MIT License
Filter object keys and values into a new object
License: MIT License
Instead of using Reflect.ownKeys
and then Object.getOwnPropertyDescriptor
(one), why not just get Object.getOwnPropertyDescriptors
(multiple) and loop that?
Similarly, you could use Object.defineProperties
instead of Object.defineProperty
to create the object in one pass after mapping the result of the previous function.
Just like map-obj and camelcase-keys.
If the input object contains an own property with the key '__proto__'
and the filter does not reject it, rather than create a corresponding own property on the target object, filterObject
sets the prototype of the target object to the property's value.
One possible fix is to use Object.defineProperty()
for creating properties, rather than assignment.
I'll submit a PR adding test cases and the proposed fix.
A consumer may want to use a Set
instead of an array for filtering keys. We could allow for iterables to be provided.
For Instance, let consider we have a JSON.
const person = {
name: "John Doe",
age: 27,
kids:[
{
name: "James Doe",
age: 7
},
{
name: "Mary Doe",
age: 7
}
]
}
how to write the filter-obj so that I can get this solution.
filterObject(person, ['name', 'kids??'])
I tested it locally neither kids.name, kids[name], kids{name}
worked!!
const person = {
name: "John Doe",
kids:[
{
name: "James Doe"
},
{
name: "Mary Doe"
}
]
}
The README
documents that symbol properties are not kept. However, this leads to some behavior that users might not expect:
const symbol = Symbol('example')
const obj = { [symbol]: true, a: 1, b: 2 }
const objCopy = excludeKeys(obj, ['b'])
objCopy[symbol] // undefined. Unexpected: the consumer only wanted to exclude 'b'
const objCopyTwo = includeKeys(obj, [symbol, 'a'])
objCopyTwo[symbol] // undefined. Unexpected: the consumer intended to keep symbol
I can submit a PR.
Something like:
const filterObject = require('filter-obj');
filterObject({a: 1, b: 1, c: 1} {keys: ['!a']});
//=> {b: 1, c: 1}
At the moment, non-enumerable properties are removed from the return value.
On one hand:
for ... in
, Object.keys(...)
, etc. which ignore non-enumerable properties. Object spreading { ...object }
itself removes non-enumerable properties.On the other hand, some users might not expect non-enumerable properties removal as a side effect of this library. For example:
excludeKeys(object, ['one'])
: this would remove object.two
if it is non-enumerable, even though the user operation only intended to exclude object.one
.includeKeys(object, ['one'])
: this would not include object.own
if it is non-enumerable, even though the user explicitly requested to include it.@sindresorhus What are your thoughts on this?
I believe lodash.pick
includes the same functionality as this module describes. I don't want to sound like hater, but I think there are enough utility libraries and reinventing the wheel isn't such a good idea.
The new properties returned by this library are always writable and configurable, even when the original properties were not. While non-writable or non-configurable properties are sometimes problematic, there are sometimes legitimate reasons for it. This seems to be an unintentional side effect.
const obj = Object.defineProperty({}, 'prop', { value: true, enumerable: true, writable: false, configurable: false })
const objCopy = includeKeys(obj, ['prop'])
console.log(Object.getOwnPropertyDescriptor(objCopy, 'prop'))
// { value: true, writable: true, enumerable: true, configurable: true }
Additionally, when a property is using get
/set
, they are currently removed.
const obj = Object.defineProperty({}, 'prop', { get: () => Math.random(), enumerable: true })
console.log(obj.prop, obj.prop) // Different random numbers
const objCopy = includeKeys(obj, ['prop'])
console.log(objCopy.prop, objCopy.prop) // Same random numbers
Instead of retrieving then copying the property values, the descriptors should be used instead.
Line 8 in 36e5f3a
Line 20 in 36e5f3a
Note: I can submit a PR.
Optional chaining has poor compatibility.
advice some compatiable method .
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.