0

I'm new to javascript and i want to complete a task using immutable js. I have a map like this:

const clients = Map({
        "c1": {
            "id": "c1",
            "isLegalEntity": false,
            "wantsEstatements": true,
            "portfolios": {
                "a": {
                    "id": "a",
                    "type": "Cash"
                },
                "b": {
                    "id": "b",
                    "type": "Margin"
                }
            }
        },
        "c2": {
            "id": "c2",
            "isLegalEntity": false,
            "wantsEstatements": true,
            "portfolios": {
                "e": {
                    "id": "e",
                    "type": "Cash"
                },
                "f": {
                    "id": "f",
                    "type": "Margin"
                }
            }
        }
    })

I want to create three tables. The first table will contain the "c1" and "c2" values so from the documentation i've read i use the clients.keys() property. The other table must contain all the portfolios id like this: ["e", "f"] and the last table must contain all the portfolios types like this: ["cash", "margin"] but i don't know how to do this from the documentation. Do you know how?

1 Answer 1

1

I'm not sure what you want to do once you have the arrays so I have simply displayed them in an HTML tag in this example. However, this should show you how to create each of the arrays you are looking for. The first step is to get the array of client IDs using the Array.from(clients.keys()); call. After this, you use the client IDs in a clients.getIn() call, which returns a standard javascript object for each client. After this, you can use standard javascript object access methods to build the arrays that you want from the client object.

var clients = Immutable.Map({
  "c1": {
    "id": "c1",
    "isLegalEntity": false,
    "wantsEstatements": true,
    "portfolios": {
      "a": {
        "id": "a",
        "type": "Cash"
      },
      "b": {
        "id": "b",
        "type": "Margin"
      }
    }
  },
  "c2": {
    "id": "c2",
    "isLegalEntity": false,
    "wantsEstatements": true,
    "portfolios": {
      "e": {
        "id": "e",
        "type": "Cash"
      },
      "f": {
        "id": "f",
        "type": "Margin"
      }
    }
  }
});

function logArray(arr) {
  var str = "[";
  for (var i = 0; i < arr.length; i++) {
    str += arr[i];
    if (i < arr.length - 1) str += ",";
  }
  str += "]"
  document.getElementById("info").innerHTML += str + "<br>";
}

var client_id_array = Array.from(clients.keys());
logArray(client_id_array);
for (var i = 0; i < client_id_array.length; i++) {
  var obj = clients.getIn([client_id_array[i]]);
  var portfolio_array = Object.keys(obj.portfolios);
  logArray(portfolio_array);
  var types = [];
  for (j = 0; j < portfolio_array.length; j++) {
    types[j] = obj.portfolios[portfolio_array[j]].type;
  }
  logArray(types);
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.7.5/immutable.js"></script>

<div id="info"></div>

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

6 Comments

awesome! Thanks!
I have a little problem here...i try to console.log the portfolio array but var portfolio_array = Object.keys(obj.portfolios); shows me the error that portfolios is undefined
I found the proper answer. I use const obj = clients.getIn([clientsIdArray[0], 'portfolios']); and then const portArray = obj.keySeq().toArray()
Are you using the same initialization for the Map as shown in your original question, or are you creating a deeper immutable object using something like Immutable.fromJS() ? The two approaches require different methods.
Im creating a deeper immutable object. It was a fault not to mention that..
|

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.