排序值不会出现在我在地图中使用排序的地方

时间:2017-11-16 11:22:50

标签: javascript

我试过下面的代码 var data =

[{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},
        {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},
        {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];     
var names = data.map(s=>s.name.sort());
console.log(names);

错误:TypeError: s.name.sort is not a function

但期望输出为["karthik","ramu", "ravi"]

6 个答案:

答案 0 :(得分:4)

您在字符串上使用了sort,但它仅适用于数组。 您首先需要获取名称,而不是在名称数组上使用排序。

在这里,您可以省略传递到sort的功能,只使用.sort(),但它会为您带来更多好处。



var data = [{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},      {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},      {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];    
        
var names = data.map(s => s.name).sort((a,b) => a.localeCompare(b));
console.log(names);




答案 1 :(得分:4)

尝试

var names = data.map(s=>s.name).sort(); //just move the sort out

<强>演示

&#13;
&#13;
var data = [{
    "name": "ramu",
    "id": "719",
    "gmail": "ramu@gmail.com",
    "ph": 988989898,
    "points": 36
  },
  {
    "name": "ravi",
    "id": "445",
    "gmail": "ravi@gmail.com",
    "ph": 4554545454,
    "points": 122
  },
  {
    "name": "karthik",
    "id": "866",
    "gmail": "karthik@gmail.com",
    "ph": 2332233232,
    "points": 25
  }
];
//var names = data.map(s=>s.name).sort(); //just move the sort out
var names = data.map(s => s.name).sort((a, b) => a.localeCompare(b)); //used localeCompare instead of simple sort

console.log(names);
&#13;
&#13;
&#13;

答案 2 :(得分:3)

正如我在上面的评论中所提到的,s.name是一个字符串,而您只能在数组上使用.sort函数。

要处理该问题,您可以首先map给定数组以获取name属性,然后然后对其进行排序。

const a = [{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},
   {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},
   {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];    
        
const names = a.map((s) => s.name).sort();

console.log(names);

答案 3 :(得分:2)

首先映射,然后对名称数组进行排序。

var data = [{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},      {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},      {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];    

var names = data.map(s=>s.name);
var sorted = names.sort();
console.log(sorted );

答案 4 :(得分:2)

首先,您可以映射数组,以便在数组中包含名称,然后可以sort

您的代码如下所示:

&#13;
&#13;
var list = [{
    "name": "ramu",
    "id": "719",
    "gmail": "ramu@gmail.com",
    "ph": 988989898,
    "points": 36
  },
  {
    "name": "ravi",
    "id": "445",
    "gmail": "ravi@gmail.com",
    "ph": 4554545454,
    "points": 122
  },
  {
    "name": "karthik",
    "id": "866",
    "gmail": "karthik@gmail.com",
    "ph": 2332233232,
    "points": 25
  }
];

var sortedNames = list.map(item => {
  return item.name;
}).sort();

console.log(sortedNames)
&#13;
&#13;
&#13;

答案 5 :(得分:1)

请参阅以下代码以获取您的名称数组 -

var data = [{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},
        {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},
        {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];

// Names Array Only
var namesArr = data.map(s => s.name).sort((a, b)=> a.localeCompare(b));
document.write('<pre>Names Array ==><br>' + JSON.stringify(namesArr) + '</pre>');

此外,如果需要,JSON按名称排序 -

var data = [{"name":"ramu","id":"719","gmail":"ramu@gmail.com","ph":988989898,"points":36},
            {"name":"ravi","id":"445","gmail":"ravi@gmail.com","ph":4554545454,"points":122},
            {"name":"karthik","id":"866","gmail":"karthik@gmail.com","ph":2332233232,"points":25}];

// sort JSON by names
data.sort(function (a, b) {
    return a.name.localeCompare(b.name);
});
document.write('<pre>Sorted JSON by names ==><br>' + JSON.stringify(data, 0, 4) + '</pre>');