基于索引排序json嵌套数组无法按预期工作

时间:2019-04-07 10:49:02

标签: javascript arrays json

我有一个json数组,我想根据其索引号对该数组进行排序

[  
   {  
      "name":"abc",
      "index":2,
      "values":[  
         {  
            "work":"three3",
            "index":3
         },
         {  
            "work":"one1",
            "index":1
         },
         {  
            "work":"two2",
            "index":2
         }
      ]
   },
   {  
      "name":"pqr",
      "index":1,
      "values":[  
         {  
            "work":"three",
            "index":3
         },
         {  
            "work":"two",
            "index":2
         },
         {  
            "work":"one",
            "index":1
         }
      ]
   }
]

我对这个数组的期望是:

[  
   {  
      "filename":"pqr",
      "children":[  
         {  
            "work":"one",
            "index":1
         },
         {  
            "work":"two",
            "index":2
         },
         {  
            "work":"three",
            "index":3
         }
      ]
   },
   {  
      "filename":"abc",
      "children":[  
         {  
            "work":"one1",
            "index":1
         },
         {  
            "work":"two2",
            "index":2
         },
         {  
            "work":"three3",
            "index":3
         }
      ]
   }
   ]

尝试如下所示。

const filterBy = (arr, childname, filterText) =>
{
    return arr.map(({filename, children}) =>
    {
        return {filename, children: children.map(({filename, children}) =>
        {
           if (filename === childname)
               return {filename, children: children.filter(
                   x => x.filename.match(filterText)
               )};
           else
               return {filename, children};
        })};
    });
}

这是一个json数组,我们无法确定它是按顺序排列的,所以我希望数组或对象应按排序顺序

但是如何在内部级别和外部级别中包含该索引并相应地对其进行排序?

2 个答案:

答案 0 :(得分:3)

您可以首先根据index对数组进行排序。这将返回一个排序数组,现在使用map。在回调函数内部获取values数组,然后再次对其进行排序。在数组映射函数内部,返回具有所需键和值的对象

let data = [{
    "name": "abc",
    "index": 2,
    "values": [{
        "work": "three3",
        "index": 3
      },
      {
        "work": "one1",
        "index": 1
      },
      {
        "work": "two2",
        "index": 2
      }
    ]
  },
  {
    "name": "pqr",
    "index": 1,
    "values": [{
        "work": "three",
        "index": 3
      },
      {
        "work": "two",
        "index": 2
      },
      {
        "work": "one",
        "index": 1
      }
    ]
  }
]


let newdt = data.sort(function(a, b) {
  return a.index - b.index
}).map(function(item) {
  let val = item.values.sort(function(a, b) {
    return a.index - b.index;
  })

  return {
    name: item.name,
    children: val
  }
})
console.log(newdt)

答案 1 :(得分:1)

首先对外部数组进行排序,使用相同的函数对内部数组进行排序

function compare(a, b) {
  if (a.index < b.index)
    return -1;
  if (a.index > b.index)
    return 1;
  return 0;
}

var objs = [{
    "name": "abc",
    "index": 2,
    "values": [{
        "work": "three3",
        "index": 3
      },
      {
        "work": "one1",
        "index": 1
      },
      {
        "work": "two2",
        "index": 2
      }
    ]
  },
  {
    "name": "pqr",
    "index": 1,
    "values": [{
        "work": "three",
        "index": 3
      },
      {
        "work": "two",
        "index": 2
      },
      {
        "work": "one",
        "index": 1
      }
    ]
  }
]


var result = objs.sort(compare).map(function(item) {
  var children = item.values.sort(compare);
  return {
    filename: item.name,
    children: children
  }
});

console.log(result);