简体   繁体   English

对嵌套的 JSON 对象数组进行排序

[英]Sort nested JSON array of objects

I have an array of objects as below.我有一个对象数组,如下所示。 I want to recursively sort this based on the key values我想根据键值递归排序

{
      "settingsList": [
        {
          "category1": [
            {
              "categoryName": "DRIVER",
              "description": "DRIVER",
              "sequence": 1
            },
            {
              "categoryName": "BALL",
              "description": "BALL",
              "sequence": 2
            },
            {
              "categoryName": "SAMPLE",
              "description": "SAMPLE",
              "sequence": 3
            },
            {
              "categoryName": "USER",
              "description": "USER",
              "sequence": 4
            }
          ]
        },
        {
          "category2": [
            {
              "paramName": "CPP",
              "description": "CPP",
              "sequence": 1
            },
            {
              "paramName": "PP",
              "description": "PP",
              "sequence": 2
            },
            {
              "paramName": "MP",
              "description": "MP",
              "sequence": 3
            }
          ]
        }
        {
          "source": {
            "instanceName": "instance_1"
          }
        }
      ]
    }

How can I efficiently sort recursively to display it sorted alphabetically on the key values.如何有效地递归排序以按字母顺序在键值上排序。 Expected output:预期 output:

{
      "settingsList": [
        {
          "category": [
            {
              "categoryName": "BALL",
              "description": "BALL",
              "sequence": 2
            },
            {
              "categoryName": "DRIVER",
              "description": "DRIVER",
              "sequence": 1
            },
            {
              "categoryName": "SAMPLE",
              "description": "SAMPLE",
              "sequence": 3
            },
            {
              "categoryName": "USER",
              "description": "USER",
              "sequence": 4
            }
          ]
        },
        {
          "category2": [
            {
              "paramName": "CPP",
              "description": "CPP",
              "sequence": 1
            },
            {
              "paramName": "MP",
              "description": "MP",
              "sequence": 3
            },
            {
              "paramName": "PP",
              "description": "PP",
              "sequence": 2
            }
          ]
        },
        {
          "source": {
            "instanceName": "instance_1"
          }
        }
      ]
    }

below is the sample code that was tried下面是尝试过的示例代码

 var object = { "settingsList": [ { "category1": [ { "categoryName": "DRIVER", "description": "DRIVER", "sequence": 1 }, { "categoryName": "BALL", "description": "BALL", "sequence": 2 }, { "categoryName": "SAMPLE", "description": "SAMPLE", "sequence": 3 }, { "categoryName": "USER", "description": "USER", "sequence": 4 } ] }, { "category2": [ { "paramName": "CPP", "description": "CPP", "sequence": 1 }, { "paramName": "PP", "description": "PP", "sequence": 2 }, { "paramName": "MP", "description": "MP", "sequence": 3 } ] }, { "source": { "instanceName": "instance_1" } } ] } var keys = Object.keys(object); var sortedKeys = keys.sort((key1, key2)=>{ key1 = key1.toLowerCase(); key2 = key2.toLowerCase(); if(key1 < key2) return -1; if(key1 > key2) return 1; return 0; }) function sortData(object){ var newObject = {}, keys = Object.keys(object); keys.sort(function(key1, key2){ key1 = key1.toLowerCase(); key2 = key2.toLowerCase(); if(key1 < key2) return -1; if(key1 > key2) return 1; return 0; }); for(var index in keys){ var key = keys[index]; if(typeof object[key] == 'object' &&;(object[key] instanceof Array)){ newObject[key] = sortData(object[key]); } else { newObject[key] = object[key]; } } return newObject. } var sortedData=sortData(object) console.log(sortedData)

.................................................................................................................................................................................................................................................................... ..................................................... ..................................................... ..................................................... ..................................................... ..................................................... …………

What you tried to achieved is called deep sort.您尝试实现的目标称为深度排序。

You can use deep-sort-object library as follow:您可以使用深度排序对象库,如下所示:

var sortobject = require('deep-sort-object');
var sortedData = sortobject(object);
console.log(sortedData);

Or if you don't want to use a library, you can use this gist as reference.或者,如果您不想使用库,则可以使用此要点作为参考。

Here is an iterative solution using object-scan这是使用对象扫描的迭代解决方案

 // const objectScan = require('object-scan'); const myData = { settingsList: [{ category1: [{ categoryName: 'DRIVER', description: 'DRIVER', sequence: 1 }, { categoryName: 'BALL', description: 'BALL', sequence: 2 }, { categoryName: 'SAMPLE', description: 'SAMPLE', sequence: 3 }, { categoryName: 'USER', description: 'USER', sequence: 4 }] }, { category2: [{ paramName: 'CPP', description: 'CPP', sequence: 1 }, { paramName: 'PP', description: 'PP', sequence: 2 }, { paramName: 'MP', description: 'MP', sequence: 3 }] }, { source: { instanceName: 'instance_1' } }] }; const sort = (data) => { const logic = { 'settingsList[*].category1': (value) => value.sort((a, b) => a.categoryName.localeCompare(b.categoryName)), 'settingsList[*].category2': (value) => value.sort((a, b) => a.paramName.localeCompare(b.paramName)) }; objectScan(Object.keys(logic), { filterFn: ({ value, matchedBy }) => { matchedBy.forEach((needle) => logic[needle](value)); } })(data); }; console.log(sort(myData)); // => undefined console.log(myData); // => { settingsList: [ { category1: [ { categoryName: 'BALL', description: 'BALL', sequence: 2 }, { categoryName: 'DRIVER', description: 'DRIVER', sequence: 1 }, { categoryName: 'SAMPLE', description: 'SAMPLE', sequence: 3 }, { categoryName: 'USER', description: 'USER', sequence: 4 } ] }, { category2: [ { paramName: 'CPP', description: 'CPP', sequence: 1 }, { paramName: 'MP', description: 'MP', sequence: 3 }, { paramName: 'PP', description: 'PP', sequence: 2 } ] }, { source: { instanceName: 'instance_1' } } ] }
 .as-console-wrapper {max-height: 100%;important: top: 0}
 <script src="https://bundle.run/object-scan@14.4.0"></script>

Disclaimer : I'm the author of object-scan免责声明:我是对象扫描的作者

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM