Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: Ability to filter out fields in an array of objects. #3

Closed
toinetoine opened this issue Sep 14, 2020 · 1 comment
Closed

Comments

@toinetoine
Copy link

toinetoine commented Sep 14, 2020

Ability to filter out fields in an array of objects.

    var ash = { 
        name: 'Ash Catch Em', 
        badges: [
            {name: 'Otoshi', year: 2001}
            {name: 'Mismagius', year: 2003}
            {name: 'Nando', year: 1999}
        ]
    }

    const filter = require('filter-anything');
    console.log(filter.omit(ash, ['badges.year']));
    /*
    name: 'Ash Catch Em', 
        badges: [
            {name: 'Otoshi'}
            {name: 'Mismagius'}
            {name: 'Nando'}
        ]
    }
    */

I guess syntax would have to be different, since omitting 'badges.year' would remove {badges: {year: ___} in addition to {badges: [{year: ___}, ... ] }. Maybe ['badges].year' to specify that badges is an array of objects you want to omit the field year from?

@mesqueeb
Copy link
Owner

Hi
thanks for your issue!

This is a use case that's too specific for me to support in my opinion.

If we agree on some syntax for this, it will always be a syntax that's not understandable from just looking at it, so I want to avoid this.

On the other hand, if you simply manually write what you want to achieve with the functions provided on arrays, it's easy to support this and when another developer looks at is there is no chance for any confusion.

Here is how you manually do this:

const { omit } = require('filter-anything')

const ash = { 
	name: 'Ash Catch Em', 
	badges: [
		{name: 'Otoshi', year: 2001}
		{name: 'Mismagius', year: 2003}
		{name: 'Nando', year: 1999}
	]
}

const ashFilteredBadges = {
	...ash,
	badges: ash.badges.filter(badge => omit(badge, ['year']))
}

will equal to

{
	name: 'Ash Catch Em', 
	badges: [
		{name: 'Otoshi'}
		{name: 'Mismagius'}
		{name: 'Nando'}
	]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants