从另一个数组中排序对象键数组

时间:2019-05-28 12:20:23

标签: javascript arrays

如何基于键合并对象数组

let data = [
  {
    "fields": [
      {
        "key": "terminal",
        "value": null
      },
      {
        "key": "status",
        "value": "Enabled"
      },
      {
        "key": "area",
        "value": null
      },
      {
        "key": "note",
        "value": "Some key notes"
      },
      {
        "key": "group",
        "value": "Medium"
      },
      {
        "key": "level",
        "value": null
      }
    ],
    "name": "test 1",
    "number": "123456127890",
    "id": "yqweyqweu213"
  },
  {
    "fields": [
      {
        "key": "terminal",
        "value": null
      },
      {
        "key": "status",
        "value": "Disabled"
      },
      {
        "key": "area",
        "value": "Delhi"
      },
      {
        "key": "note",
        "value": "Some key orginal notes"
      },
      {
        "key": "group",
        "value": "Medium"
      },
      {
        "key": "level",
        "value": "High"
      }
    ],
    "name": "test 2",
    "number": "173276123612",
    "id": "uqweyewueyyuqwe"
  },
  {
    "fields": [
      {
        "key": "terminal",
        "value": 1
      },
      {
        "key": "status",
        "value": "Enabled"
      },
      {
        "key": "area",
        "value": "Mumbai"
      },
      {
        "key": "note",
        "value": "Some key orginal sample notes"
      },
      {
        "key": "group",
        "value": "Low"
      },
      {
        "key": "level",
        "value": null
      }
    ],
    "name": "test 3",
    "number": "128737812381723",
    "id": "kasjdashdkaskjd"
  }
]

const orderArr = [
  "area",
  "terminal",
  "note",
  "status",
  "group",
  "level"
]

尝试了以下代码段

 data.forEach(o => {
  let keys = []
  o.fields.forEach(f => {
    keys.push(f.key)
  })
  let sortedKeys =  keys.sort(function(a, b) {
    return orderOfFields.indexOf(a) - orderOfFields.indexOf(b);
   })
   let obj = {}
   sortedKeys.forEach(s => {
     obj[s] = // how to get the value
   })
   return obj
 })

expectedResult是

  let expectedData = [
    {
      "fields": [
        {
          "key": "area",
          "value": null
        },
        {
          "key": "terminal",
          "value": null
        },
        {
          "key": "note",
          "value": "Some key notes"
        },
        {
          "key": "status",
          "value": "Enabled"
        },
        {
          "key": "group",
          "value": "Medium"
        },
        {
          "key": "level",
          "value": null
        }
      ],
      "name": "test 1",
      "number": "123456127890",
      "id": "yqweyqweu213"
    },
    {
      "fields": [
        {
          "key": "area",
          "value": "Delhi"
        },
        {
          "key": "terminal",
          "value": null
        },

        {
          "key": "note",
          "value": "Some key orginal notes"
        },
        {
          "key": "status",
          "value": "Disabled"
        },

        {
          "key": "group",
          "value": "Medium"
        },
        {
          "key": "level",
          "value": "High"
        }
      ],
      "name": "test 2",
      "number": "173276123612",
      "id": "uqweyewueyyuqwe"
    },
    {
      "fields": [
        {
          "key": "area",
          "value": "Mumbai"
        },
        {
          "key": "terminal",
          "value": 1
        },
        {
          "key": "note",
          "value": "Some key orginal sample notes"
        },
        {
          "key": "status",
          "value": "Enabled"
        },

        {
          "key": "group",
          "value": "Low"
        },
        {
          "key": "level",
          "value": null
        }
      ],
      "name": "test 3",
      "number": "128737812381723",
      "id": "kasjdashdkaskjd"
    }
  ]

1 个答案:

答案 0 :(得分:1)

您不必先获取sortedKeys即可创建一个对象。您可以在sort的compareFunction中直接使用indexOf。如果a.keyorderArr中的索引比b.key小,则减法将返回-1,并且a将放在b的前面。

const data=[{"fields":[{"key":"terminal","value":null},{"key":"status","value":"Enabled"},{"key":"area","value":null},{"key":"note","value":"Some key notes"},{"key":"group","value":"Medium"},{"key":"level","value":null}],"name":"test 1","number":"123456127890","id":"yqweyqweu213"},{"fields":[{"key":"terminal","value":null},{"key":"status","value":"Disabled"},{"key":"area","value":"Delhi"},{"key":"note","value":"Some key orginal notes"},{"key":"group","value":"Medium"},{"key":"level","value":"High"}],"name":"test 2","number":"173276123612","id":"uqweyewueyyuqwe"},{"fields":[{"key":"terminal","value":1},{"key":"status","value":"Enabled"},{"key":"area","value":"Mumbai"},{"key":"note","value":"Some key orginal sample notes"},{"key":"group","value":"Low"},{"key":"level","value":null}],"name":"test 3","number":"128737812381723","id":"kasjdashdkaskjd"}]
const orderArr=["area","terminal","note","status","group","level"]

data.forEach(o => 
  o.fields.sort((a, b) => orderArr.indexOf(a.key) - orderArr.indexOf(b.key))
)

console.log(data)

如果您想要一个新的数组而不改变原始的arary,则可以像这样使用map

const data=[{"fields":[{"key":"terminal","value":null},{"key":"status","value":"Enabled"},{"key":"area","value":null},{"key":"note","value":"Some key notes"},{"key":"group","value":"Medium"},{"key":"level","value":null}],"name":"test 1","number":"123456127890","id":"yqweyqweu213"},{"fields":[{"key":"terminal","value":null},{"key":"status","value":"Disabled"},{"key":"area","value":"Delhi"},{"key":"note","value":"Some key orginal notes"},{"key":"group","value":"Medium"},{"key":"level","value":"High"}],"name":"test 2","number":"173276123612","id":"uqweyewueyyuqwe"},{"fields":[{"key":"terminal","value":1},{"key":"status","value":"Enabled"},{"key":"area","value":"Mumbai"},{"key":"note","value":"Some key orginal sample notes"},{"key":"group","value":"Low"},{"key":"level","value":null}],"name":"test 3","number":"128737812381723","id":"kasjdashdkaskjd"}]
const orderArr=["area","terminal","note","status","group","level"]

const output = data.map(o => ({ 
  ...o, 
  fields: [...o.fields].sort((a, b) => orderArr.indexOf(a.key) - orderArr.indexOf(b.key)) 
}))

console.log(output)