0

I have a json data set with arrays.I want to retrieve data from that set to another array.

this is my json data

[
    {
        "user": [
            {
                "userActivityId": "08d66e8f-0f2f-3162-4d1d-b0307df0b532",
                "userId": "08d65df8-5a1c-4879-b8b6-f583bafa8084",
                "userName": "Dorothy",
                "photoUrl": "08d65df8-5a1c-d841-cff6-444e72fa08f2",
                "createdOn": "2018-12-31T01:13:23.363154",
                "itemType": 0,
                "itemId": "08d66e8f-0f2f-2efa-db7a-ab313ff89e7c"
            },
            {
                "userActivityId": "08d66e8f-19cf-7c9c-1e07-00c9e7c37fc0",
                "userId": "08d65df8-5a1c-4879-b8b6-f583bafa8084",
                "userName": "Dorothy",
                "photoUrl": "08d65df8-5a1c-d841-cff6-444e72fa08f2",
                "createdOn": "2018-12-31T01:13:41.190886",
                "itemType": 1,
                "itemId": "08d66e8f-19cf-a711-47a1-9690d9f6bf5e"
            }
        ]
    },
    {
        "user": [
            {
                "userActivityId": "08d66e3f-61e0-55db-f57d-5d434738d366",
                "userId": "08d6630e-325b-3474-5b5c-3c07af8063b8",
                "userName": "Supun",
                "photoUrl": "1",
                "createdOn": "2018-12-30T15:43:02.314203",
                "itemType": 0,
                "itemId": "08d66e3f-61df-c79d-dac4-f16f1142d526"
            }
        ]
    }
]

What I want to do is,I want to get this data set inside user array values to single array.

This is the way I followed,

friendsStufs: any[];
 this.userService.getFriendsStufs(this.authService.currentUser.id).subscribe(data => {
  data.forEach(user => {
    user.forEach(stufs => {
      this.friendsStufs.push(stufs);
    });
  });
}, error => {
  console.log(error);
  this.alertify.error('Can retrieve friends stufs');
});

1 Answer 1

1

You can use array#reduce to accumulate all user object in a single array.

let data = [ { "user": [ { "userActivityId": "08d66e8f-0f2f-3162-4d1d-b0307df0b532", "userId": "08d65df8-5a1c-4879-b8b6-f583bafa8084", "userName": "Dorothy", "photoUrl": "08d65df8-5a1c-d841-cff6-444e72fa08f2", "createdOn": "2018-12-31T01:13:23.363154", "itemType": 0, "itemId": "08d66e8f-0f2f-2efa-db7a-ab313ff89e7c" }, { "userActivityId": "08d66e8f-19cf-7c9c-1e07-00c9e7c37fc0", "userId": "08d65df8-5a1c-4879-b8b6-f583bafa8084", "userName": "Dorothy", "photoUrl": "08d65df8-5a1c-d841-cff6-444e72fa08f2", "createdOn": "2018-12-31T01:13:41.190886", "itemType": 1, "itemId": "08d66e8f-19cf-a711-47a1-9690d9f6bf5e" } ] }, { "user": [ { "userActivityId": "08d66e3f-61e0-55db-f57d-5d434738d366", "userId": "08d6630e-325b-3474-5b5c-3c07af8063b8", "userName": "Supun", "photoUrl": "1", "createdOn": "2018-12-30T15:43:02.314203", "itemType": 0, "itemId": "08d66e3f-61df-c79d-dac4-f16f1142d526" } ] } ],
    result = data.reduce((r,{user}) => {
      user.forEach(o => r.push({...o}));
      return r;
    },[]);
console.log(result);

Sign up to request clarification or add additional context in comments.

2 Comments

can you please explain what is the meaning of 3 dots inside push method? r.push({...o})
... is spread syntax, it is used to create a new object here.

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.