Javascript 如何更好地构造 JSON 中的数组数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4730696/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 13:49:16  来源:igfitidea点击:

How to better structure an Array of Arrays in JSON

javascriptarraysjson

提问by Rigil

In the following JSON object:

在以下 JSON 对象中:

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

回答by user113716

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

它需要对其进行重组,以便消除“会计/销售”属性并创建employees一个对象数组。

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

示例: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
    }
] 

回答by Jimmy Chandra

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.

你不能这样旋转。要么将部门作为员工对象中的键移动,要么必须像员工.accounting[0].firstName 一样访问它。

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

如果你坚持以employees[index]的身份访问employee,你必须将其重构为:

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

回答by fazo

From jsFiddle

来自jsFiddle

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

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