arraysreactjsobjectakita

Add key/value on array object with map


I would add a key value/value on my array objects "members".

I am on React JS with Akita.

addAge(): Observable<Member[]> {
return this.selectAll().pipe(
                map((allMembers) => ({...allMembers, age:10}))
            )
}

-

 dataQuery.addDatasWeek().subscribe(res => {
                this.setState({obj: res})
            })

I hope for membersWithAge :

[{id: 5, name: "Teddy, age: 10}, {id: 6, name: "Toto", age: 10},...]

but...

enter image description here


Solution

  • You're adding age to the allMembers object, you need to add it to each member inside allMembers.

    addAge(): Observable < Member[] > {
      return this.selectAll().pipe(
        map((allMembers) => allMembers.map(member => ({
          ...member,
          age: 10
        })))
      )
    }