1

i'm new to react and javascript and i have some difficulties coping with a task. Let's get started. I have and array like this: clients = ["Alex", "Jimmy"] and then i create another array with the following way:

for (var i = 0; i < clients.length; i++) {
  var portf = Object.keys(obj.portfolios);
}

As a result for every i a portf array is created with the following values: For i = 0 portf = 1,2,3 and for i = 1 portf = 4,5,6 I want to create and array of objects with the following format using iteration:

var ops = [{

    label: "Alex",
    options: [
       {value: 1}, 
       {value: 2}, 
       {value: 3}
    ],
    label: 'Jimmy',
    options: [
       {value: 4}, 
       {value: 5}, 
       {value: 6}
    ]

Do you know how to do this?

2
  • Object.keys(obj.portfolios) is going to be the same for every i. How do you get 1,2,3 and 4,5,6? Commented Mar 19, 2017 at 18:53
  • i have a deeply nested map structure. The clients array contains the keys of the map. The portf is created with this procedure: for (var i = 0; i < clients.length; i++) { var obj = clients.getIn([clients[i]]); var portf = Object.keys(obj.portfolios); and as a result for i=0 the porf has values of 1,2,3 and for i=1 values of 4,5,6 Commented Mar 19, 2017 at 18:55

1 Answer 1

1

You can use Array.prototype.map:

const opts = clients.map(client => {
  const obj = clients.getIn([client]);
  const portf = Object.keys(obj.portfolios);

  return {
    label: client,
    options: portf.map(value => ({ value }))
  }
})
Sign up to request clarification or add additional context in comments.

1 Comment

Nice! Thanks a lot. It was easier than i thought :P

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.