Skip to content

NoemiRozpara/removeNested

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

Remove element from array of objects

Helper for removing objects using computed nested key names.

Before:

const users = [...this.state.users];
const userIndex = users.length
  ? users.findIndex(user => user.name && user.name.first === 'John')
  : -1;
userIndex > -1 && users.splice(userIndex, 1);
this.setState({ users });

After:

const users = removeFromArray(this.state.users, 'name.first', 'John');
this.setState({ users });
  • Works for unlimited keys chain length (accepts string, can be single key name or names seperated by dots)
  • If element wasn't found returns original array
  • If element was found, returns new array - immutable function

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published