1

I have a Javascript object with the following format assigned to a javascript variable:

var events = 

[
   {
      "Id":20,
      "CustomerId":9,
      "CustomerName":"Mark Wikkins",
      "Code":"CT6789",
      "CustomerDate":"\/Date(1466679600000)\/",
      "Levels":[
         {
            "Id":92,
            "Nivel":0,
            "Code1":"Sales",
            "Code2":null,
            "Description":"Customer",
         },
         {
            "Id":94,
            "Nivel":0,
            "Code1":"Sales",
            "Code2":null,
            "Description":"Customer",
         }
      ]
   },
   {
      "Id":21,
      "CustomerId":14,
      "CustomerName":"John Stweart",
      "Code":"CT70000",
      "CustomerDate":"\/Date(146667970000)\/",
      "Levels":[
         {
            "Id":102,
            "Nivel":0,
            "Code1":"Jobs",
            "Code2":null,
            "Description":"Customer",       
         },
         {
            "Id":106,
            "Nivel":"0",
            "Code1":"Commissions",
            "Code2":null,
            "Description":"Customer",
         },
         {
            "Id":113,
            "Nivel":0,
            "Code1":"Organizations",
            "Code2":null,
            "Description":"Customer",
         }
      ]
   }
];

And I have a drop down with the following Text and Values

<select name="customers_select" id="customers_select">
  <option value="92">Sales</option>
  <option value="106">Commisions</option>
  <option value="113">Organizations</option>
</select>

If I wanted to get the CustomerDate upon selecting from the drop down, what would be the best way to do it?

As you can see the values of the select (dropdown) map to a Level Id, but the level is a property of the parent object in the Javascript Object.

So if I select "Commisions" from the drop down, i need to evaluate my variable events and obtain the CustomerDate for the second object.

Is there something like I could get the CustomerDate by passing the value of the select from the events Javascript Object array?.

jedgard
  • 868
  • 3
  • 23
  • 41
  • Just to be pedantic, that's not "a json" - it's a JavaScript object. – Pointy Apr 24 '14 at 04:21
  • thank you for the correction Pointy – jedgard Apr 24 '14 at 04:21
  • `events[0].CustomerDate` ? – adeneo Apr 24 '14 at 04:22
  • @adeneo i only have access to the level id from the drop down... – jedgard Apr 24 '14 at 04:22
  • 1
    How do you know which "CustomerID" is relevant? – Pointy Apr 24 '14 at 04:23
  • Then you have to iterate until you find the right id – adeneo Apr 24 '14 at 04:23
  • @Pointy, I need to get the CustomerDate for the selected level Id from the drop down. The CustomerID is irrelevant for my situation. No two Customers will have the same level id's. But without going into the requirements details, I just would like to know the best way to obtain the CustomerDate based on the selection from the dropdown. – jedgard Apr 24 '14 at 04:27
  • I was hoping there was a better way than iterating through the whole list to find it but if that is the only option, i will do it that way. I am kind of new to javascript so i thought there might have been a better way. – jedgard Apr 24 '14 at 04:30
  • might help: => http://stackoverflow.com/questions/11930638/how-can-i-access-the-properties-of-my-json-object-using-jquery-in-this-jsfiddle – Tats_innit Apr 24 '14 at 04:32

1 Answers1

2

Two ways you can do this.

Create a Hash table

Loop through your JavaScript object and map Level_ids to CustomerDates like this:

var hash = {};
hash["level-102"] = "\/Date(146667970000)\/";
hash["level-106"] = "\/Date(146667970000)\/";
hash["level-113"] = "\/Date(146667970000)\/";
... and so on

You can do that by looping through your object with jQuery's .each() function (and you'll need another one inside that to traverse the Level object).

In the JavaScript select event, you can take the id and make a string "level-" + id and look up in your hash table: var customerDate = hash["level-" + id];

Search your JavaScript object each time

Keep your JavaScript object as-is, and when the user selects from the drop-down, it fires an event handler that traverses through your complex object looking for the level_id. While doing this it is remembering the current "CustomerDate" so when it is found, you break out of the search loop.

Michael Butler
  • 6,079
  • 3
  • 38
  • 46