使用lodash对数组对象数据进行排序

对数组对象数据进行排序处理过程:
1、安装lodash依赖

npm i lodash -S

2,引入lodash

let _ = require('lodash');

3、数据排序(升序)

let features = [
      {
        code: '11',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ],
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ]

          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '02',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ],
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ]

          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '01',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '03',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '07',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '05',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '06',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '04',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
    ];


  let result = _.sortBy(features,function(item){
    return item.code;//根据code对数据进行升序排序,如果降序则改为:return -item.code
  });
  console.log(result);

4、结果输出为:

 [
      {
        code: '01',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ],
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ]

          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '02',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ],
            [
              [117.9362647,24.6866522],
              [118.0078597,24.6877492],
              [118.0067403,24.759389],
              [117.9351229,24.7582472]
            ]

          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '03',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '04',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '05',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '06',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '07',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
      {
        code: '11',
        type: 'Feature',
        geometry: {
          type: 'Polygon',
          coordinates: [
            [
              [117.9652977,24.5483274],
              [117.9787306,24.5483274],
              [117.9785567,24.562019],
              [117.9651456,24.5617725]
            ]
          ]
        },
        properties: {
          OBJECTID: 1,
          CODE: '1'
        }
      },
    ]

你可能感兴趣的:(lodash,vue)