2016-12-29 120 views
0

我目前正在尝试按属性属性的值对对象属性进行排序,如果这样做合理的话。通过嵌套属性的值排序对象属性

var obj = { 
    1: { name: "Mike", surname: "Smith"}, 
    2: { name: "Albert", surname: "Einstein"}, 
    3: { name: "Steve", surname: "Jobs"} 
} 

说我想按姓氏这些属性的顺序进行排序,以便最终的结果是

var obj = { 
    2: { name: "Albert", surname: "Einstein"}, 
    3: { name: "Steve", surname: "Jobs"}, 
    1: { name: "Mike", surname: "Smith"} 
} 

当然必须有比把所有的姓氏成这样其他的一种优雅的方式数组排序,然后重建对象。

+0

您可以使用可排序但不是实际不是对象的数组。 –

+0

@NinaScholz你的意思是:你可以使用一个数组,它可以排序,但不是'object',实际上不是。 – ozil

+1

[在JavaScript中通过字符串属性值对对象数组排序]可能的重复(http://stackoverflow.com/questions/1129216/sort-array-of-objects-by-string-property-value-in-javascript) –

回答

4

请试试这个代码:

var arr = [ 
 
    { 
 
     f_name: 'George', 
 
     l_name: 'Washington', 
 
     age: 279 
 
    }, 
 
    { 
 
     f_name: 'Abraham', 
 
     l_name: 'Lincoln', 
 
     age: 202 
 
    }, 
 
    { 
 
     f_name: 'Barack', 
 
     l_name: 'Obama', 
 
     age: 50 
 
    } 
 
]; 
 

 
$(function() { 
 
    $('#headings th').click(function() { 
 
     var id = $(this).attr('id'); 
 
     var asc = (!$(this).attr('asc')); // switch the order, true if not set 
 
     
 
     // set asc="asc" when sorted in ascending order 
 
     $('#headings th').each(function() { 
 
      $(this).removeAttr('asc'); 
 
     }); 
 
     if (asc) $(this).attr('asc', 'asc'); 
 
     
 
     sortResults(id, asc); 
 
    }); 
 
     
 
    showResults(); 
 
}); 
 

 
function sortResults(prop, asc) { 
 
    arr = arr.sort(function(a, b) { 
 
     if (asc) return (a[prop] > b[prop]); 
 
     else return (b[prop] > a[prop]); 
 
    }); 
 
    showResults(); 
 
} 
 

 
function showResults() { 
 
    var html = ''; 
 
    for (var e in arr) { 
 
     html += '<tr>' 
 
      +'<td>'+arr[e].f_name+'</td>' 
 
      +'<td>'+arr[e].l_name+'</td>' 
 
      +'<td>'+arr[e].age+'</td>' 
 
     +'</tr>'; 
 
    } 
 
    $('#results').html(html); 
 
}
table { 
 
    margin: 3px; 
 
} 
 
table th { 
 
    font-weight: bold; 
 
    cursor: pointer; 
 
} 
 
table th, table td { 
 
    padding: 3px; 
 
    border: 1px solid #000; 
 
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
 
Click on the table headings to sort the results. 
 
<table> 
 
    <thead id="headings"> 
 
     <tr> 
 
      <th id="f_name">First Name</th> 
 
      <th id="l_name">Last Name</th> 
 
      <th id="age">Age</th> 
 
     </tr> 
 
    </thead> 
 
    <tbody id="results"> 
 
     <!-- this will be auto-populated --> 
 
    </tbody> 
 
</table>

+0

这段代码工作,但仍然继承原始变量是一个数组,Im实际上使用angularJS并试图对这个对象使用ng-repeat,我知道它只适用于数组。 – Ernie

1

你需要的是有一个顺序的对象。这可能是ArrayMap

然后您需要考虑您的数据模式(即从服务器发出的JSON数据的形状)。我可能会将ID移动到对象本身中(替代方法是使其隐含,这很脆弱且容易出错)。

[ 
    { 
    "id": 1, 
    "name": "Fred", 
    ... 
    } 
] 

在这一点上,它只是一个排序问题。

Array有一个sort function on the prototype

注意如果需要执行转换到的阵列,可以使用Array.from

var obj = { 
 
    1: { name: "Mike", surname: "Smith" }, 
 
    2: { name: "Albert", surname: "Einstein" }, 
 
    3: { name: "Steve", surname: "Jobs" }, 
 
    length: 4 // largest *zero-based* index 
 
}; 
 

 
console.log(Array.from(obj));

所得阵列将是稀疏的(有间隙),那么你将需要筛选结果以消除它们:

Array.from(obj).filter(populated); 

function populated(v) { 
    return !!v; 
} 
+0

我担心这是唯一的方法,但我认为我可以让它工作。这个副本被ng-repeat使用,只有在那里,所以我猜想将地图改变为一个地图阵列,因为这不是世界的尽头。 – Ernie