根据 String 属性对对象数组进行排序

我有这样的数据结构。

[
        {"name": "John", "gender": "Male", "id": "1"},
        {"name": "Max", "gender": "Male", "id": "2"},
        {"name": "John", "gender": "Male", "id": "3"},
        {"name": "Sam", "gender": "Male", "id": "4"},
        {"name": "Max", "gender": "Male", "id": "5"},
        {"name": "Sam", "gender": "Male", "id": "6"},
        {"name": "Sam", "gender": "Male", "id": "7"},
        {"name": " ", "gender": "Male", "id": "8"},
        {"name": "John", "gender": "Male", "id": "9"},
        {"name": " ", "gender": "Male", "id": "10"},
    ]

我正在尝试根据属性对其进行排序name。我需要所有记录name=max应该首先出现name=Sam,然后name=John是具有空名称字段或任何其他值的其余记录。

我尝试了不同的技术,但没有得到想要的结果。

有人可以建议我如何实现这一目标吗?

谢谢。


梦里花落0921
浏览 119回答 4
4回答

呼唤远方

您可以给每个name案例打分,然后在Array.sort函数中使用该分数。const data = [  { name: 'John', gender: 'Male', id: '1' },  { name: 'Max', gender: 'Male', id: '2' },  { name: 'John', gender: 'Male', id: '3' },  { name: 'Sam', gender: 'Male', id: '4' },  { name: 'Max', gender: 'Male', id: '5' },  { name: 'Sam', gender: 'Male', id: '6' },  { name: 'Sam', gender: 'Male', id: '7' },  { name: ' ', gender: 'Male', id: '8' },  { name: 'John', gender: 'Male', id: '9' },  { name: ' ', gender: 'Male', id: '10' }];const getScore = name => {  if (name === 'Max') return 3;  else if (name === 'Sam') return 2;  else if (name === 'John') return 1;  else return 0;};data.sort((a, b) => getScore(b.name) - getScore(a.name));console.log(data);

牛魔王的故事

您可以执行以下操作,data = [&nbsp; &nbsp; &nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "1"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "Max", "gender": "Male", "id": "2"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "3"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "4"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "Max", "gender": "Male", "id": "5"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "6"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "7"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": " ", "gender": "Male", "id": "8"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "9"},&nbsp; &nbsp; &nbsp; &nbsp; {"name": " ", "gender": "Male", "id": "10"},&nbsp; &nbsp; ]data.sort((a, b) => {&nbsp; if(a.name < b.name) {&nbsp; &nbsp; return 1;&nbsp; } else if(a.name > b.name) {&nbsp; &nbsp; return -1;&nbsp; }&nbsp; return 0;})console.log(data);

泛舟湖上清波郎朗

如果您的数据包含区分大小写的名称怎么办?const getScore = name => {&nbsp; if (name.toLowerCase() === 'max') return 3;&nbsp; else if (name.toLowerCase() === 'sam') return 2;&nbsp; else if (name.toLowerCase() === 'john') return 1;&nbsp; else return 0;};data.sort((a, b) => getScore(b.name) - getScore(a.name));console.log(data);

料青山看我应如是

您好,您可以使用此代码var arr = [&nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "1"},&nbsp; &nbsp; {"name": "Max", "gender": "Male", "id": "2"},&nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "3"},&nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "4"},&nbsp; &nbsp; {"name": "Max", "gender": "Male", "id": "5"},&nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "6"},&nbsp; &nbsp; {"name": "Sam", "gender": "Male", "id": "7"},&nbsp; &nbsp; {"name": " ", "gender": "Male", "id": "8"},&nbsp; &nbsp; {"name": "John", "gender": "Male", "id": "9"},&nbsp; &nbsp; {"name": " ", "gender": "Male", "id": "10"},];var arr1 = arr.filter((value, index) => {&nbsp; &nbsp; if(value.name == 'Max'){&nbsp; &nbsp; &nbsp; &nbsp; return value&nbsp; &nbsp; }});var arr2 = arr.filter((value, index) => {&nbsp; &nbsp; if(value.name != 'Max'){&nbsp; &nbsp; &nbsp; &nbsp; return value&nbsp; &nbsp; }});arr2 = arr2.sort((a, b) => (a.name < b.name) ? 1 : -1);var output = arr1.concat(arr2);console.log(output);
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript