0

I have an array like this.

[
    {"Test": "1", "Recommendedby": "3,4,5,6"},
    {"ABC": "2", "Recommendedby": "1,2,3"},
    {"Cvb": "3", "Recommendedby": ""}
]

Now I need to get the Recommendedby column and I want to this column data means I need to send each number in recommended column data to service to get the username of that userid.

Please tell me how to achieve this.

3
  • your js object is not valid Commented Nov 12, 2018 at 14:49
  • can you provide expected array Commented Nov 12, 2018 at 14:51
  • This might be what you're after: stackoverflow.com/questions/18804592/… Commented Nov 12, 2018 at 14:53

2 Answers 2

0

To achieve expected use below option of looping through array with forEach

  1. Use forEach to loop through array
  2. Use split with comma to create another array for Recommendedby
  3. Use anoher forEach to loop through Recommendedby array

let arr = [{"Test":1,"Recommendedby":"3,4,5,6"},{"ABC":"2","Recommendedby":"1,2,3"},{"Cvb":"3","Recommendedby":""}]

arr.forEach(v => v.Recommendedby.split(',').forEach(val => {
  console.log("Recommended by id-", val); // make service call here to pass each id
}))

codepen - https://codepen.io/nagasai/pen/XyNgEG?editors=1010

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

Comments

0

Try like this :

let array = [
    {"Test":1,"Recommendedby":"3,4,5,6"},
    {"ABC":"2","Recommendedby":"1,2,3"},
    {"Cvb":"3","Recommendedby":""}
]

array.map(item => item.Recommendedby.split(',').map(id => {

// this.http.getUser(id).subscribe(...)

}))

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.