代码之家  ›  专栏  ›  技术社区  ›  dns_nx

Javascript-使用lodash对数组排序-从初始变量获取密钥

  •  0
  • dns_nx  · 技术社区  · 7 年前

    我相信,这是一个简单的问题,但实际上我找不到解决办法。也许有人能给我个暗示。

    我有这种类型的对象数组:

    const Country = {
        albania:
        {
            'iso':'al',
            'countryNo': 70,
            'name': 
            {
                de: 'Albanien',
                en: 'Albania',
                pl: 'Albania',
            },
            'flag':'flag-icon-al',
            'showCountry':true,
        },
        austria:
        {
            'iso':'at',
            'countryNo': 38,
            'name': 
            {
                de: 'Österreich',
                en: 'Austria',
                pl: 'Austria',
            },
            'flag':'flag-icon-at',
            'showCountry':true,
        },
        belgium:
        {
            'iso':'be',
            'countryNo': 2,
            'name': 
            {
                de: 'Belgien',
                en: 'Belgium',
                pl: 'Belgia',
            },
            'flag':'flag-icon-be',
            'showCountry':true,
        },
    ...
    

    这个物体的钥匙是 albania , austria 等 我现在要对数组进行排序。这是我正在做的 lodash sortBy 作用

    let countryList = _.sortBy(Country,'name[' + this.props.currentLanguage + ']');
    

    当我遍历 countryList 数组,如何从 Country 对象数组,即。 阿尔巴尼亚 ?

    我试着和他一起工作 map 函数,但我只得到名为 0 , 1

    countryList.map((country,key) => {
        // country -> has all object information
        // key -> 0, 1, 2 etc.
    })
    

    请参见调试图片:

    enter image description here

    使现代化

    还有比这更好的解决方案吗:

    countryList.map((country,key) => {
        var key = Object.keys(Country).find(function(element) {
            const countryInner = Country[element];
            if(countryInner.countryNo === country.countryNo) {
                return element;
            }
        });
        if(country.showCountry === true) {
            return (
                <HashLink to="/#locations" className={"dropdown-item imageLink animated"} onClick={(e) => this.handleClickCountry(e, key)} key={"nav" + key}>{country.name[this.props.currentLanguage].toUpperCase()}</HashLink> 
            );
        }
    })
    
    2 回复  |  直到 7 年前
        1
  •  0
  •   Shyam Tayal    7 年前

    我建议的解决方案是,不要使用lodash,而是用对象创建一个2d数组。基本上是将它们移动到一个数组中,对该数组进行排序,然后重建

    var countryList = [];
    for (var con in Country) {
        countryList.push([con, Country[con]]);
    }
    //Sort based on key value
    sortable.sort(function(a, b) {
      return a[0] > b[0];
    });
    

    一旦有了数组,就可以按照喜欢的顺序从数组中重建对象。这样你的密钥就不会丢失,你也不必重复两次才能找到它。 希望有帮助

        2
  •  0
  •   Poyraz Yilmaz    7 年前

    在对集合进行排序时,您将丢失对象键,因此,如果排序后保留这些键对您很重要,您可以将这些键添加到对象中,作为您可以使用的属性 map

    let countries = _.map(Country, (country, key) => {
        country.key = key;
        return country;
    });
    

    现在我们可以用 sortBy 洛达斯法

    let orderedCountries = _.sortBy(countries, function (country) {
        return country.name[lang]
    });
    

    哪里 lang 是你的语言之一('de'm'en','pl')。。。

    我相信你也希望根据他们的喜好筛选你的列表 showCountry 属性,让我们合并所有属性。。。

    function sortAndFilterCountries(countries, lang) {
        countries = _.map(countries, (country, key) => {
            country.key = key;
            return country;
        });
        let orderedCountries = _.sortBy(countries, function (country) {
            return country.name[lang]
        });
        return _.filter(orderedCountries, 'showCountry');
    }