Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何对包含json对象组的json数组进行排序_Javascript_Arrays_Json_Sorting - Fatal编程技术网

Javascript 如何对包含json对象组的json数组进行排序

Javascript 如何对包含json对象组的json数组进行排序,javascript,arrays,json,sorting,Javascript,Arrays,Json,Sorting,我想根据name属性对下面的json数组进行排序。挑战在于,如果“rows”包含两个json数组元素,那么它应该首先在组内短接,然后根据另一个json数组进行排序 这是我的尝试。它对数组进行排序,但不对(组内的元素)进行排序 首先,您可以通过以下方式对行数组进行排序: data.forEach(item=>item.rows.sort((row1,row2)=>row1.name.localeCompare(row2.name)); 然后,您可以根据行数组对数据的元素进行排序。例如,如果要根据

我想根据name属性对下面的json数组进行排序。挑战在于,如果“rows”包含两个json数组元素,那么它应该首先在组内短接,然后根据另一个json数组进行排序

这是我的尝试。它对数组进行排序,但不对(组内的元素)进行排序


首先,您可以通过以下方式对
数组进行排序:

data.forEach(item=>item.rows.sort((row1,row2)=>row1.name.localeCompare(row2.name));
然后,您可以根据
数组对
数据
的元素进行排序。例如,如果要根据第一行的
名称对它们进行排序:

data.sort((item1,item2)=>item1.rows[0].name.localeCompare(item2.rows[0].name));
// sort the data
this.leadPoolRecords = JSON.parse(
  JSON.stringify(this.leadPoolRecords)
).sort((a, b) => {
  a = a[colName] ? a[colName].toLowerCase() : ""; // Handle null values
  b = b[colName] ? b[colName].toLowerCase() : "";
  return a > b ? 1 * isReverse : -1 * isReverse;
});

data =    [
      {
        "button": {
          "btn": true,
          "size": "20"
        },
        "rows": [
          {
            "name": "john",
            "role": "CEO",
            "phone": "xxxxxxxxx",
            "email": "bcd@email.com"
          }
        ]
      },
      {
        "button": {
          "btn": true,
          "size": "20"
        },
        "rows": [
          {
            "name": "Mike",
            "role": "Director",
            "phone": "xxxxxxxxx",
            "email": "cde@email.com"
          }
        ]
      },
      {
        "button": {
          "btn": true,
          "size": "20"
        },
        "rows": [
          {
            "name": "rahul",
            "role": "CTO",
            "phone": "xxxxxxxxx",
            "email": "xyz@email.com"
          },
          {
            "name": "ajay",
            "role": "Researcher",
            "phone": "xxxxxxxxx",
            "email": "abc@email.com"
          }
        ]
      }
    ]