按特定键值对几个数组排序

时间:2018-11-26 16:56:02

标签: javascript lodash

我的api返回了以下对象

country {Code: "AT", Country: "Austria", Nationality: "Austrian"}
country {Code: "BE", Country: "Belgium", Nationality: "Belgian"}
country {Code: "CH", Country: "Switzerland", Nationality: "Swiss"}
country {Code: "DE", Country: "Germany", Nationality: "German"}
country {Code: "ES", Country: "Spain", Nationality: "Spanish"}

我需要按“国家”而不是“代码”的顺序返回这些信息。

这是我的代码

this.countries = _.map(list, (item) => {
    const code = item.iso;
    const country = _.find(countries, (country) => country.Code == code);

    //const maryPoppins = _.sortBy(country, ['country.Country']);
    //console.log('maryPoppins',maryPoppins)

    //returns the above objects
    console.log('country', country)

    return {
        code: code,
        name: country
    }
});

我尝试了sortBy和orderBy(破折号),但我认为我需要将每个对象合并到一个新数组中,然后返回结果/对结果进行排序。

任何帮助表示赞赏

1 个答案:

答案 0 :(得分:0)

您可以使用zoo::na.locf对字符串进行排序

localeCompare

相关问题