https://github.com/arfeo/find-and
Find nested objects and: appendProps / replaceObject / changeProps / removeObject / returnFound / insertObjectBefore / insertObjectAfter. TypeScript friendly. Plug & play.
https://github.com/arfeo/find-and
append find nested-objects remove replace
Last synced: 1 day ago
JSON representation
Find nested objects and: appendProps / replaceObject / changeProps / removeObject / returnFound / insertObjectBefore / insertObjectAfter. TypeScript friendly. Plug & play.
- Host: GitHub
- URL: https://github.com/arfeo/find-and
- Owner: arfeo
- License: mit
- Created: 2019-12-07T16:56:00.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2024-05-21T09:14:39.000Z (over 1 year ago)
- Last Synced: 2025-10-31T23:16:14.984Z (3 months ago)
- Topics: append, find, nested-objects, remove, replace
- Language: TypeScript
- Homepage:
- Size: 372 KB
- Stars: 19
- Watchers: 1
- Forks: 2
- Open Issues: 9
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# find-and
[](https://travis-ci.org/arfeo/find-and)
Find nested objects and:
* `appendProps`: append props to the found object.
* `replaceObject`: replace all props of the found object.
* `changeProps`: replace some existing props of the found object.
* `removeObject`: remove the found object.
* `returnFound`: get the found object, or an object array if there's more than one object found.
* `insertObjectBefore`: insert an object before the found object if the found object's parent is array.
* `insertObjectAfter`: insert an object after the found object if the found object's parent is array.
## Installation
```shell script
$ npm i find-and
```
## Examples
Say, we have an object array like:
```js
const data = [
{
id: 1,
name: 'One',
children: [
{
id: 2,
name: 'Two',
children: [
{
id: 3,
name: 'Three',
},
{
id: 4,
name: 'Four',
},
],
},
],
},
{
id: 5,
name: 'Five',
},
];
```
1. The result of `appendProps(data, { id: 5 }, { description: 'Blah' })`:
```
[
{
id: 1,
name: 'One',
children: [
{
id: 2,
name: 'Two',
children: [
{
id: 3,
name: 'Three',
},
{
id: 4,
name: 'Four',
},
],
},
],
},
{
id: 5,
name: 'Five',
description: 'Blah',
},
]
```
1. The result of `replaceObject(data, { id: 3 }, { id: 30 })`:
```
[
{
id: 1,
name: 'One',
children: [
{
id: 2,
name: 'Two',
children: [
{
id: 30,
},
{
id: 4,
name: 'Four',
},
],
},
],
},
{
id: 5,
name: 'Five',
},
]
```
1. The result of `changeProps(data, { id: 2 }, { name: 'Foo' })`:
```
[
{
id: 1,
name: 'One',
children: [
{
id: 2,
name: 'Foo',
children: [
{
id: 3,
name: 'Three',
},
{
id: 4,
name: 'Four',
},
],
},
],
},
{
id: 5,
name: 'Five',
},
]
```
1. The result of `removeObject(data, { id: 3 })`:
```
[
{
id: 1,
name: 'One',
children: [
{
id: 2,
name: 'Two',
children: [
{
id: 4,
name: 'Four',
},
],
},
],
},
{
id: 5,
name: 'Five',
},
]
```
The behavior is quite the same for an object.
```js
const data = {
name: 'One',
description: 'Description',
children: [
{
id: 1,
name: 'Two',
},
{
id: 2,
name: 'Three',
},
],
};
```
In this case, the result of `changeProps(data, { name: 'One' }, { name: 'Foo' })`:
```
{
name: 'Foo',
description: 'Description',
children: [
{
id: 1,
name: 'Two',
},
{
id: 2,
name: 'Three',
},
],
}
```