React Javascript-基于内部嵌套数组值排序数组
我有一个数组,需要根据特定React Javascript-基于内部嵌套数组值排序数组,javascript,arrays,reactjs,sorting,nested,Javascript,Arrays,Reactjs,Sorting,Nested,我有一个数组,需要根据特定rotaTypeId值的sortPosition值进行排序 当前的rotaTypeId是1ebe7766-4a6b-4157-a998-00ebae24d662,因此预期结果将显示Sally第一,Joe第二 我尝试在外部数组上循环,并为每个元素获取其rotaTypeAccountEmployees内部数组。然后检查当前的rotaTypeId值是否匹配 这就是我得到的,因为我不确定如何返回更新/排序的数组 这是我的阵列: [ { "object":
rotaTypeId
值的sortPosition
值进行排序
当前的rotaTypeId
是1ebe7766-4a6b-4157-a998-00ebae24d662
,因此预期结果将显示Sally第一,Joe第二
我尝试在外部数组上循环,并为每个元素获取其rotaTypeAccountEmployees
内部数组。然后检查当前的rotaTypeId
值是否匹配
这就是我得到的,因为我不确定如何返回更新/排序的数组
这是我的阵列:
[
{
"object": "accountEmployee",
"accountEmployeeId": "c80b2d75-6091-423c-b51b-41cef265046a",
"employee": {
"object": "employee",
"employeeId": "c3832cff-ac4c-4133-ad29-a00ca8fd25f6",
"firstName": "Joe",
"surname": "Bloggs",
"email": "joe@bloggs.com"
},
"salary": 16286.40,
"hourlyRate": 7.83,
"weeklyContractHours": 40,
"rotaTypeAccountEmployees": [
{
"object": "rotaTypeAccountEmployee",
"rotaTypeId": "1ebe7766-4a6b-4157-a998-00ebae24d662",
"sortPosition": 2
},
{
"object": "rotaTypeAccountEmployee",
"rotaTypeId": "01d8ec46-d1cf-49e2-b992-840dfdb03a83",
"sortPosition": 1
}
]
},
{
"object": "accountEmployee",
"accountEmployeeId": "bdde68a4-7df0-431b-b108-db5c26ca7208",
"employee": {
"object": "employee",
"employeeId": "724c4c4c-978d-4f62-9345-28219153e728",
"firstName": "Sally",
"surname": "Bloggs",
"email": "sally@bloggs.com"
},
"salary": 16286.40,
"hourlyRate": 7.83,
"weeklyContractHours": 40,
"rotaTypeAccountEmployees": [
{
"object": "rotaTypeAccountEmployee",
"rotaTypeId": "1ebe7766-4a6b-4157-a998-00ebae24d662",
"sortPosition": 1
},
{
"object": "rotaTypeAccountEmployee",
"rotaTypeId": "01d8ec46-d1cf-49e2-b992-840dfdb03a83",
"sortPosition": 2
}
]
}
]
试着跟随
let arr=[{“object”:“accountEmployee”,“accountEmployeeId”:“c80b2d75-6091-423c-b51b-41cef265046a”,“employee”:“object”:“employeeId”:“c3832cff-ac4c-4133-ad29-a00ca8fd25f6”,“firstName”:“Joe”,“姓氏”:“Bloggs”,“email”:joe@bloggs.com“},“工资”:16286.4,“小时工资”:7.83,“每周合同工时”:40,“轮换会计员工”:[{“对象”:“rotaTypeAccountEmployee”、“rotaTypeId”:“1ebe7766-4a6b-4157-a998-00ebae24d662”、“sortPosition”:2},{“object”:“rotaTypeAccountEmployee”、“rotaTypeId”:“01d8ec46-d1cf-49e2-b992-840dfdb03a83”、“sortPosition”:1}],{“object”:“accountEmployee”、“accountEmployeeId”:“bdde68a4-7df0-431b-b108-DB265CCA7208”、“employeeId”:“employeeId”:“employee”:“object”:”724C-978d-4f62-9345-28219153e728,“名字”:“莎莉”,“姓氏”:“博客”,“电子邮件”:sally@bloggs.com“},“工资”:16286.4,“小时工资”:7.83,“每周合同时间”:40,“RotatypeCountEmployee”:[{“对象”:“RotatypeCountEmployee”,“rotaTypeId”:“1ebe7766-4a6b-4157-a998-00ebae24d662”,“sortPosition”:1},{“对象”:“RotatypeCountEmployee”,“rotaTypeId”:”01d8ec46-d1cf-49e2-b992-840dfdb03a83,“排序位置”:2}]}];
让id=“1ebe7766-4a6b-4157-a998-00ebae24d662”;
arr.sort((a,b)=>{
返回a.rotaTypeAccountEmployees.find({rotaTypeId})=>id==rotaTypeId.sortPosition-b.rotaTypeAccountEmployees.find({rotaTypeId})=>id==rotaTypeId.sortPosition
});
console.log(arr);
您可以使用Ramda库,并通过函数组合轻松实现:
const sortByKey = '1ebe7766-4a6b-4157-a998-00ebae24d662';
const result = R.sortBy(
R.pipe(
R.prop('rotaTypeAccountEmployees'),
R.filter(R.propEq('rotaTypeId', sortByKey)),
R.path(['0', 'sortPosition']),
),
)(data);
我认为Nikhil Aggarwal解决方案更快、更短,但最好了解一些算法实现,这里是一个快速排序实现:
var criteria = "1ebe7766-4a6b-4157-a998-00ebae24d662";
function partition(arr, low, high)
{
var pivot = getValue(arr[high], criteria);
var i = (low-1);
for (let j=low; j<high; j++)
{
if (getValue(arr[j], criteria) <= pivot)
{
i++;
let temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
var temp = arr[i+1];
arr[i+1] = arr[high];
arr[high] = temp;
return i+1;
}
function quickSort(arr, low,high)
{
if (low < high)
{
let pi = partition(arr, low, high);
quickSort(arr, low, pi-1);
quickSort(arr, pi+1, high);
}
}
function getValue(element, criteria){
let value = 0;
element.rotaTypeAccountEmployees.map(o=>{
if(o.rotaTypeId==criteria){
value = o.sortPosition;
}
});
return value;
}
quickSort(arr,0,arr.length-1);
console.log(arr);
var标准=“1ebe7766-4a6b-4157-a998-00ebae24d662”;
功能分区(arr、低、高)
{
var pivot=getValue(arr[高],标准);
var i=(低-1);
对于(让j=low;jplease add:这是我尝试过的代码…@NinaScholz我已经添加了我尝试过的代码。因此这个解决方案非常接近。似乎是排序rotaTypeAccountEmployees
数组,而不是外部数组?@SeanDelaney-它只排序外部数组。请重新检查并给出一个您发现它不起作用的场景。S对不起,我的错。它工作正常。谢谢你的帮助。.find(({rotaTypeId}=>id===rotaTypeId)
是我在尝试编写自己的解决方案/打开这个问题寻求帮助之前需要的一点,所以我今天学到了一些新东西!@SeanDelaney-很高兴帮助你:)