【问题标题】:How to better structure an Array of Arrays in JSON如何在 JSON 中更好地构造数组数组
【发布时间】:2011-06-11 11:15:57
【问题描述】:

在以下 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

如何重组对象,以便可以像这样访问每个员工的名字:

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

【问题讨论】:

  • 不是 JSON 对象。它是对象文字表示法。 benalman.com/news/2010/03/theres-no-such-thing-as-a-json 。除此之外,是否要将salesaccounting 合并为一个数组?
  • 是的,我想要 1 个包含多个数组的数组。
  • 在您的示例employees[0].firstName 中,您有一个对象数组。不是数组数组。你的意思是对象数组吗?
  • 是的,我需要一个对象数组。谢谢

标签: javascript arrays json


【解决方案1】:

这需要对其进行重组,以便消除“会计/销售”属性并使employees 成为对象数组。

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

【讨论】:

    【解决方案2】:

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

    如果你坚持以雇员[index]的身份访问员工,你必须将其重组为:

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

    并引入一种不同的按部门过滤的方式。

    也许创建一个循环遍历员工数组的函数,并将每个匹配过滤器的元素复制到一个新的数组对象中并返回它。

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

    【讨论】:

      【解决方案3】:

      来自jsFiddle

      var employees = { "firstName" : "John",  // First element
                        "lastName"  : "Doe",
                        "age"       : 23 },
      
                      { "firstName" : "Mary",  // Second Element
                        "lastName"  : "Smith",
                        "age"       : 32 }
                       ;
      alert(employees);
      

      【讨论】:

      • 如果您要给出答案,请在此处发布,而不是在其他网站上发布。
      • @patrick, @fazo - 或者我会把它贴在两个地方,以防 jsfiddle 失败。
      • @Chaos:是的,我的意思不是完全不包含链接,但答案应该在这里给出。前几天,jsFiddle 出现了一段时间,提问者需要参考上一个问题中的解决方案。不幸的是,所有对该问题的答案都是 jsFiddle 链接!本网站的实用性不应受制于另一个网站的停机时间。
      猜你喜欢
      • 1970-01-01
      • 2018-12-03
      • 2018-10-02
      • 1970-01-01
      • 2015-11-25
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多