1
[
  {
    "details": {
      "name": "john",
      "point": "20"
    },

    "list": {
      "number": "30",

    }
  },
  {
    "details": {
      "name": "doe",
      "point": "25"
    },
    "list": {
      "number": "30",

    }
  }
]

This is what i am trying to do, i am getting the data from the store and if the response is only one i use data = getData[0].details and if the response is more than one then i push the data using [...data, ...getData[1].details] if there are more than one data how can i achieve. thanks in advance

 let data:any = [];
     this.store
              .pipe(
                select(getData),
                map((getData,i) => {
                  if (getData) {
                      data = [...data, ...getData[i].details]
                  }
             return data;
                })
             )
4
  • I'm very confused what are trying to do here, can you elaborate a bit more in your question? Please include an example of what the desired output would be. Also, I don't understand how select(getData) can work, isn't getData defined only inside map's callback? Commented Mar 8, 2020 at 23:35
  • 1
    I assume getData is the result of some API callback Commented Mar 9, 2020 at 0:10
  • But then it wouldn't exist in select(getData) Commented Mar 9, 2020 at 0:15
  • Yes select(getdata) is the response from api Commented Mar 9, 2020 at 2:23

2 Answers 2

1

I think you want to get an array of the details. In this case you can say.

let data = [
  {
    "details": {
      "name": "john",
      "point": "20"
    },

    "list": {
      "number": "30",

    }
  },
  {
    "details": {
      "name": "doe",
      "point": "25"
    },
    "list": {
      "number": "30",

    }
  }
];

let details = data.map(a => a.details);

console.log(details);

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

Comments

1

I think i understand what you mean:

What I would do is map over your getData response and add to the original array on each iteration. It wont matter if there is 1 or many in the getData array:

getData.map(x => $data.push(x.details));

Comments

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.