对嵌套的json复杂数组进行排序

时间:2018-04-17 06:49:07

标签: javascript sorting object lodash

我搜索如何按place.city这种具有密钥id的对象进行排序。需要保留第一把钥匙的ID ......

{
    "123": {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "Bob",
            "age": 45
        }
    },
    "456": {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Louis",
            "age": 34
        }
    },
    "789": {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Kevin",
            "age": 27
        }
    }
}

我尝试某种类似的功能,预期的结果不在这里。

let result = _(myObject).map(function (value, key) {
    return _.defaults({ name: key }, value)
}).sortBy('city').value()

5 个答案:

答案 0 :(得分:1)

您无法对对象进行排序。但是,您可以将对象转换为数组并对其进行排序。

var data ={
  "123" : {
    "place": {
      "city": "New York",
      "country": "USA" 
    },
    "person": {
      "name": "Bob",
      "age": 45
    }
  },
  "456" : {
    "place": {
      "city": "Chicago",
      "country": "USA" 
    },
    "person": {
      "name": "Louis",
      "age": 34
    }
  },
  "789" : {
    "place": {
      "city": "Dallas",
      "country": "USA" 
    },
    "person": {
      "name": "Kevin",
      "age": 27
    }
  }
};

var sortedByPlace = _.sortBy(Object.keys(data).map(k => ({id:k, ...data[k]})), (d)=> d.place.city)

console.log(sortedByPlace);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.5/lodash.min.js"></script>

答案 1 :(得分:1)

无法对对象进行排序,您需要将其作为列表。

import { map, flow } from 'lodash'
import { sortBy } from 'lodash/fp'

cities => flow(
  map(places, (place, id) => { id, ...place }),
  sortBy('city'),
)()

如果你想进行本地排序,你的第二个问题就会回答问题(mh ......)。那将是

import { mapValues } from 'lodash'
import { sortBy } from 'lodash/fp'

data => mapValues(data, sortBy('place.city'))

答案 2 :(得分:0)

如果您查看this答案,以下内容应该有效

var sort = function (prop, arr) {
    prop = prop.split('.');
    var len = prop.length;

    arr.sort(function (a, b) {
        var i = 0;
        while( i < len ) { a = a[prop[i]]; b = b[prop[i]]; i++; }
        if (a < b) {
            return -1;
        } else if (a > b) {
            return 1;
        } else {
            return 0;
        }
    });
    return arr;
};
sort("place.city", myObject);

答案 3 :(得分:0)

你无法对一个物体进行排序。但是,您可以创建一个包含对象引用的排序数组:

  const sorted = Object.values(myObject).sort((a, b) => a.place.city.localeCompare(b.place.city));

答案 4 :(得分:0)

我意识到我要处理的对象(从一个阻碍API获取)比我的第一个例子稍微复杂一点:/

所以你很好地给出的回答不再适用了......

你看到这个新物体的微妙之处吗?

place.city

排序
{
    "123": {
      0: {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "Bob",
            "age": 45
        }
      },
      1: {
        "place": {
            "city": "New York",
            "country": "USA" 
        },
        "person": {
            "name": "James",
            "age": 32
        }
      }
    },
    "456": {
      0: {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Louis",
            "age": 34
        }
      },
      1: {
        "place": {
            "city": "Chicago",
            "country": "USA" 
        },
        "person": {
            "name": "Christine",
            "age": 65
        }
      }
    },
    "789": {
      0: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Kevin",
            "age": 27
        }
      },
      1: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Robert",
            "age": 55
        }
      },
      2: {
        "place": {
            "city": "Dallas",
            "country": "USA" 
        },
        "person": {
            "name": "Danny",
            "age": 62
        }
      }
    }
}