1

I have a data like below:

const Users = 
{
  "users": [
    {
      "id": "person1",
      "information": [
        {
        "first_name": "Mike",
        "last_name": "Patty",
        "address": ["address1","address2"]
        },
        {
          "first_name": "Mike2",
          "last_name": "Patty2",
          "address": ["address1","address2"]
        }
      ]
    },
    {
      "id": "person2",
      "information": [
        {
        "first_name": "Tom",
        "last_name": "Jerry",
        "address": ["address1","address2"]
        },
        {
          "first_name": "Tom2",
          "last_name": "Jerry2",
          "address": ["address1","address2"]
        }
      ]
    }
  ]
}
  1. I would like to iterate users and add each user's information to the API database. addInfo request requires: first_name, last_name and address (address is array).

  2. Each this.APIservice.addInformation() will return one response, I will need the response later

How do I collect all response from all information for the same users and get in a format like:

// should return two object/array, each object/array collects all response from information for the same person.
person1:
["response from Mike Patty", "response from Mike2 Patty2"]
person2:
["response from Tom Jerry", "response from Tom2 Jerry2"]

here is what I am trying to code so far:

// add information 
const addUsers$ = forkJoin(

  Users.users.map(person => 
    person.information.map(info => 
      this.APIservice.addInformation(
        {
         firstName: info.first_name,
         lastName: info.last_name,
         address: info.address
        })
      )
    )
  );

otherFunction$. // this needs to run first
  .pipe(
  mergeMap(() => addUsers$)
  tap(console.log),
  // I need to manipulate res later here
  ).subscribe(() => console.log("finish"))
}

The error with current codes:

TypeError: You provided 'undefined' where a stream was expected. You can provide an Observable, Promise, Array, or Iterable.

Thank you in advance!

1 Answer 1

1

You can iterate it like this.

Users.users.map(person=>{
      person.information.map(info => 
      this.APIservice.addInformation(
        {
         firstName: info.first_name,
         lastName: info.last_name,
         address: info.address
        })
      )
    )
    })
Sign up to request clarification or add additional context in comments.

2 Comments

Shouldn't that use Users.users.map ?
@Rohit Singh Thanks for answering. This will not work. It gives me 2 undefined in console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.