【问题标题】:Sorting an object in an array through a method通过方法对数组中的对象进行排序
【发布时间】:2019-09-07 12:02:16
【问题描述】:

此函数对数组进行排序。首先,应该显示国家等于第二个参数中指定的对象。然后剩下的。但我不明白哪里出了问题?

 
const arr = [
  {  
"name":"BMW",
"price":"55 000",
"country":"Germany",
"certificate":"yes"
  },
  {  
"name":"Mitsubishi",
"price":"93 000", 
"constructor":"Bar John",
"door":"3",
"country":"Japan",
  },
  {  
"name":"Mercedes-benz",
"price":"63 000", 
"country":"Germany",
"certificate":"yes"
  },
  {  
"name":"TOYOTA", 
"price":"48 000", 
"max_people":"7",
"country":"Japan",
"certificate":"yes"
  },
  {  
"name":"Volkswagen",
"price":"36 000", 
"constructor":"Pier Sun",
"country":"Germany",
"certificate":"no"
  },
 ];

 function countries(arr,country){
  let one =  arr.sort(function(item){
return country > item.country;
  });
  return one;
}

console.log(countries(arr,"Japan"));

【问题讨论】:

    标签: javascript arrays sorting object methods


    【解决方案1】:

    您需要将这两个对象与通缉的国家/地区进行比较,然后将此项目移至顶部。

    Array#sort 改变数组的顺序。

    function countries(array, country) {
        return array.sort(({ country: a }, { country: b }) => (b === country) - (a === country));
    }
    
    const array = [{ name: "BMW", price: "55 000", country: "Germany", certificate: "yes" }, { name: "Mitsubishi", price: "93 000", constructor: "Bar John", door: "3", country: "Japan" }, { name: "Mercedes-benz", price: "63 000", country: "Germany", certificate: "yes" }, { name: "TOYOTA", price: "48 000", max_people: "7", country: "Japan", certificate: "yes" }, { name: "Volkswagen", price: "36 000", constructor: "Pier Sun", country: "Germany", certificate: "no" }];
    
    console.log(countries(array, "Japan"));
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 感谢您的精彩回答!我也有同样的问题。为了兼容 IE,我将您的代码调整为:return array.sort(function(a, b) { return (b.country === country) - (a.country === country));
    【解决方案2】:

    确保sort的回调有两个参数,并稍微改变你的排序逻辑:

     
    
    const arr = [
      {  
    "name":"BMW",
    "price":"55 000",
    "country":"Germany",
    "certificate":"yes"
      },
      {  
    "name":"Mitsubishi",
    "price":"93 000", 
    "constructor":"Bar John",
    "door":"3",
    "country":"Japan",
      },
      {  
    "name":"Mercedes-benz",
    "price":"63 000", 
    "country":"Germany",
    "certificate":"yes"
      },
      {  
    "name":"TOYOTA", 
    "price":"48 000", 
    "max_people":"7",
    "country":"Japan",
    "certificate":"yes"
      },
      {  
    "name":"Volkswagen",
    "price":"36 000", 
    "constructor":"Pier Sun",
    "country":"Germany",
    "certificate":"no"
      },
     ];
    
     function countries(arr,country){
      let one =  arr.sort(function(a, b){
    return a == country ? -1 : b == country ? 1 : 0;
      });
      return one;
    }
    
    console.log(countries(arr,"Japan"));

    【讨论】:

      【解决方案3】:

      Array.sort 中的比较函数应该返回:

      • 值 > 0 表示 a > b
      • 值 == 0 表示 a == b

      所以最简单的方法就是改成:

      let one = arr.sort(function(item) {
          return (country > item.country) ? 1 : -1
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-02-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-12-11
        • 1970-01-01
        相关资源
        最近更新 更多