开发者

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


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
    }
] 


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;
}


From jsFiddle

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

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜