Rigil
Rigil

Reputation: 549

How to better structure an Array of Arrays in JSON

In the following JSON object:

var employees = { "accounting" : [   // accounting is an array in employees.
                                { "firstName" : "John",  // First element
                                  "lastName"  : "Doe",
                                  "age"       : 23 },

                                { "firstName" : "Mary",  // Second Element
                                  "lastName"  : "Smith",
                                  "age"       : 32 }
                              ], // End "accounting" array.                                  
              "sales"       : [ // Sales is another array in employees.
                                { "firstName" : "Sally", // First Element
                                  "lastName"  : "Green",
                                  "age"       : 27 },

                                { "firstName" : "Jim",   // Second Element
                                  "lastName"  : "Galley",
                                  "age"       : 41 }
                              ] // End "sales" Array.
            } // End Employees

How do I restructure the object so I can access each employee first name like this:

employees[0].firstName
employees[1].firstName
// etc

Upvotes: 9

Views: 64992

Answers (3)

user113716
user113716

Reputation: 322452

It would require restructuring it so that you'd eliminate the "accounting/sales" properties and make employees an Array of Objects.

Example: http://jsfiddle.net/hgMXw/

var employees = [
    {
    "dept": "accounting", // new property for this object
    "firstName": "John",
    // First element
    "lastName": "Doe",
    "age": 23
    },
    {
    "dept": "accounting", // new property for this object
    "firstName": "Mary",
    // Second Element
    "lastName": "Smith",
    "age": 32
    },
    {
    "dept": "sales", // new property for this object
    "firstName": "Sally",
    // Third Element
    "lastName": "Green",
    "age": 27
    },
    {
    "dept": "sales", // new property for this object
    "firstName": "Jim",
    // Fourth Element
    "lastName": "Galley",
    "age": 41
    }
] 

Upvotes: 16

fazo
fazo

Reputation: 1827

From jsFiddle

var employees = { "firstName" : "John",  // First element
                  "lastName"  : "Doe",
                  "age"       : 23 },

                { "firstName" : "Mary",  // Second Element
                  "lastName"  : "Smith",
                  "age"       : 32 }
                 ;
alert(employees);

Upvotes: 0

Jimmy Chandra
Jimmy Chandra

Reputation: 6580

You can't pivot this like that. Either you move the department as a key in the employee object or you have to access it like employees.accounting[0].firstName.

If you insist on accessing the employee as employees[index], you have to restructure it to:

var employees = [
  { "firstName" : "John", "lastName" : "Doe", "age" : 23, "department" : "accounting" },
  { "firstName" : "...", ..., "department" : "accounting" },
  ... and so on.
];

and introduce a different way to filter by department.

maybe create a function that loop through the employees array, and copy each element that match the filter into a new array object and return it.

function getAllEmployeesFilteredBy(filterName, filterValue, empArray)
{
  var result = [];
  for (var i=0; i < empArray.length; i++) {
    if (empArray[i][filterName] === filterValue)
      //by ref
      result[result.length] = empArray[i];

      //or if you prefer by value (different object altogether)
      //result[result.length] = { "firstName" : empArray[i].firstName, "lastName" : empArray[i].lastName, ... }
  }
  return result;
}

Upvotes: 0

Related Questions