[英]Sort javascript array of objects on subobject key
I have an array of objects containing sub-objects like this;我有一个包含这样子对象的对象数组;
var array=[
{
"EmployeeName": "John",
"Experience": "12",
"data":{
"Technology":"SharePoint"
}
},
{
"EmployeeName": "Charles",
"Experience": "9",
"data":{
"Technology":"ASPNET"
}
},
{
"EmployeeName": "Jo",
"Experience": "3",
"data":{
"Technology":"PHP"
}
},
{
"EmployeeName": "Daine",
"Experience": "7",
"data":{
"Technology":"javascript"
}
},
{
"EmployeeName": "Zain",
"Experience": "6",
"data":{
"Technology":"Unity"
}
}
];
Now, I want to sort this array based on the sub-objects key "Technology".现在,我想根据子对象键“技术”对这个数组进行排序。 I am using the example from here: https://jsfiddle.net/KSPriyaranjan/4Lzr0qux
我正在使用这里的示例: https://jsfiddle.net/KSPriyaranjan/4Lzr0qux
This does the job when it sorts from a base object, but how do I get this to work:当它从基础 object 排序时,它就可以完成这项工作,但我该如何让它工作:
function GetSortOrder(prop){
return function(a,b){
if( a[prop] > b[prop]){
return 1;
}else if( a[prop] < b[prop] ){
return -1;
}
return 0;
}
}
array.sort( GetSortOrder("data.Technology") );
document.write("<br><br> Sorted Technology Names : <br>");
for (var item in array) {
document.write("<br>"+array[item].Technology);
}
This produces this:这会产生:
Sorted Technology Names :
undefined
undefined
undefined
undefined
Hope someone can help in this matter and thanks in advance:-) undefined希望有人可以在这件事上提供帮助,并提前感谢:-) undefined
If the property passed contains any .
如果传递的属性包含任何
.
s, use reduce
to navigate to the last nested value before comparison: s,使用
reduce
导航到比较之前的最后一个嵌套值:
const GetSortOrder = (accessor) => {
const getVal = accessor.includes('.')
? outer => accessor.split('.').reduce((a, prop) => a[prop], outer)
: outer => outer;
return (a, b) => getVal(a).localeCompare(getVal(b));
};
var array=[ { "EmployeeName": "John", "Experience": "12", "data":{ "Technology":"SharePoint" } }, { "EmployeeName": "Charles", "Experience": "9", "data":{ "Technology":"ASPNET" } }, { "EmployeeName": "Jo", "Experience": "3", "data":{ "Technology":"PHP" } }, { "EmployeeName": "Daine", "Experience": "7", "data":{ "Technology":"javascript" } }, { "EmployeeName": "Zain", "Experience": "6", "data":{ "Technology":"Unity" } } ]; const GetSortOrder = (accessor) => { const getVal = accessor.includes('.')? outer => accessor.split('.').reduce((a, prop) => a[prop], outer): outer => outer; return (a, b) => getVal(a).localeCompare(getVal(b)); }; array.sort( GetSortOrder("data.Technology") ); console.log(array);
The conditional isn't strictly necessary (though you may consider it to make the intent clearer), you could also do条件不是绝对必要的(尽管您可能认为它使意图更清晰),您也可以这样做
const getVal = outer => accessor.split('.').reduce((a, prop) => a[prop], outer)
Technology is not a property of your object.技术不是您的 object 的财产。 It is a property of your object.data.
它是您的 object.data 的属性。
Try changing尝试改变
javascript array[item].Technology
to至
array[item].data.Technology
You could add a function for getting the value by keeping your commparing approach.您可以添加一个 function 通过保持比较方法来获得价值。
function GetSortOrder(prop) { const keys = prop.split('.'), getValue = object => keys.reduce((o, k) => o[k], object); return function(a, b) { const left = getValue(a), right = getValue(b); if (left > right) return 1; else if (left < right) return -1; return 0; } } var array = [{ EmployeeName: "John", Experience: "12", data: { Technology: "SharePoint" } }, { EmployeeName: "Charles", Experience: "9", data: { Technology: "ASPNET" } }, { EmployeeName: "Jo", Experience: "3", data: { Technology: "PHP" } }, { EmployeeName: "Daine", Experience: "7", data: { Technology: "javascript" } }, { EmployeeName: "Zain", Experience: "6", data: { Technology: "Unity" } }]; array.sort(GetSortOrder("data.Technology")); console.log(array);
.as-console-wrapper { max-height: 100%;important: top; 0; }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.