Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
Adeelasd committed May 20, 2021
1 parent d8efffc commit 7901e37
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,16 @@ gets specific values from Array of objects.
``` let data=[{ name:"foo" , id:1 },{ name:"bar" , id:2 }] ```

``` sort(data,"name") // { foo:{ id:1 , name:"foo" }, bar:{ id:2 , name:"bar" } } ```
<!--

### mergeUp
converts array of objects into an object based on key provided.
``` sort(Array<Object>,String) ```
merges child object with the parent object.
``` mergeUp(Object,String) ```
#### e.g
``` let data=[{ name:"foo" , id:1 },{ name:"bar" , id:2 }] ```
``` let data ={ test:22 , foo:"aas" , val:{ foo:"aa" , bar:"bb" } } ```

``` mergeUp(data,"val") // { test: 22, foo: 'aa', bar: 'bb' } ```

``` sort(data,"name") // { foo:{ id:1 , name:"foo" }, bar:{ id:2 , name:"bar" } } ``` -->
``` mergeUp(data,"val",false) // { test: 22, foo: 'aas', bar: 'bb' } ```


### exceptObj
Expand Down
19 changes: 9 additions & 10 deletions src/Object/mergeUp.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
const mergeUp = (data: { [key: string]: any }, value: string, overwrite = false) => {
const mergeUp = (data: { [key: string]: any }, value: string, overwrite = true) => {
let ref = Object.assign({}, data)
delete ref[value]
let keys = Object.keys(data[value])
if(overwrite)
for (let items of keys) {
ref[items] = data[value][items]
}
if (overwrite)
for (let items of keys) {
ref[items] = data[value][items]
}
else
for (let items of keys) {
if(!data[items])
ref[items] = data[value][items]
}

for (let items of keys) {
if (!data[items])
ref[items] = data[value][items]
}
return ref
}

Expand Down

0 comments on commit 7901e37

Please sign in to comment.