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.

Thanks!

share|improve this question
3  
This is not a JSON object. It is object literal notation. benalman.com/news/2010/03/theres-no-such-thing-as-a-json . Beside that, do you want to merge sales and accounting into one array? – Felix Kling Jan 19 '11 at 0:53
Yes, I want 1 array that contains several arrays. – Rigil Jan 19 '11 at 0:55
In your example employees[0].firstName, you have one array of objects. Not an array of arrays. Do you mean array of objects? – Felix Kling Jan 19 '11 at 0:56
Yes, an array of objects is what I need. Thanks – Rigil Jan 19 '11 at 0:58

3 Answers

up vote 3 down vote accepted

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}
] 
share|improve this answer
This was what I was looking for. Thanks Patrick – Rigil Jan 19 '11 at 1:01
@Rigil: You're welcome. – user113716 Jan 19 '11 at 1:02

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;
}
share|improve this answer

From jsFiddle

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

                { "firstName" : "Mary",  // Second Element
                  "lastName"  : "Smith",
                  "age"       : 32 }
                 ;
alert(employees);
share|improve this answer
If you're going to give an answer, please post it here instead of on another site. – user113716 Jan 19 '11 at 1:03
@patrick, @fazo - Alternatively I would post it in both places in case jsfiddle ever goes under. – ChaosPandion Jan 19 '11 at 1:06
@Chaos: Yes, I didn't mean to not include a link at all, but the answer ought to be presented here. The other day, jsFiddle was down for a bit and an asker needed to reference the solutions in a previous question. Unfortunately all the answers given to that question were jsFiddle links! The usefulness of this site shouldn't be subject to the downtime of another. – user113716 Jan 19 '11 at 1:09

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.