React Native 基于数组的排序字典
React Native Sort Dictionary Based on Array
我有一个包含
的“参考”数组
[“Push”,”Pull, “Leg”]
和一组看起来像
的字典
{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "pull"
},
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "push"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
},
我希望数组根据参考数组的 PPL 属性进行排序。
因此,由于引用数组是 push, pull, leg
,字典数组应该看起来像
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "push"
},
{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "pull"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
},
根据引用数组上ppl
属性的索引使用Javascriptsort
函数(可以使用函数indexOf
得到这个索引)。这里是可爱的one-liner:
// Reference array
const arrRef = ["Push", "Pull", "Leg"];
// Dictionary array (order "Push - Leg - Push")
const arrDicts = [
{ "ppl": "Push" },
{ "ppl": "Leg" },
{ "ppl": "Push" },
];
// Sorting (should be "Push - Push - Leg")
arrDicts.sort((a, b) => arrRef.indexOf(a.ppl) - arrRef.indexOf(b.ppl));
编辑:添加旧的建议解决方案(备份阵列)
const arrRef = ["Push", "Pull", "Leg"];
const arrDicts = [
{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "Push"
},
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "Pull"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "Leg"
},
];
// Create a back up array and iterate on the reference array, filtering the actual dicitionary array with `ppl == current reference`
const backupArr = [];
for (const ref of arrRef)
backupArr.push(...arrDicts.filter(x => x.ppl == ref))
// Now backupArr is ordered as you want to
console.log(backupArr)
请考虑到这种排序数组的方式将不包括字典数组中具有不在参考数组中的 ppl
属性的对象。
我有一个包含
的“参考”数组[“Push”,”Pull, “Leg”]
和一组看起来像
的字典{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "pull"
},
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "push"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
},
我希望数组根据参考数组的 PPL 属性进行排序。
因此,由于引用数组是 push, pull, leg
,字典数组应该看起来像
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "push"
},
{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "pull"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "legs"
},
根据引用数组上ppl
属性的索引使用Javascriptsort
函数(可以使用函数indexOf
得到这个索引)。这里是可爱的one-liner:
// Reference array
const arrRef = ["Push", "Pull", "Leg"];
// Dictionary array (order "Push - Leg - Push")
const arrDicts = [
{ "ppl": "Push" },
{ "ppl": "Leg" },
{ "ppl": "Push" },
];
// Sorting (should be "Push - Push - Leg")
arrDicts.sort((a, b) => arrRef.indexOf(a.ppl) - arrRef.indexOf(b.ppl));
编辑:添加旧的建议解决方案(备份阵列)
const arrRef = ["Push", "Pull", "Leg"];
const arrDicts = [
{
"bodyPart": "upper arms",
"target": "biceps",
"broad_target": "biceps",
"ppl": "Push"
},
{
"bodyPart": "chest",
"target": "pectorals",
"broad_target": "chest",
"ppl": "Pull"
},
{
"bodyPart": "lower legs",
"target": "calves",
"broad_target": "legs",
"ppl": "Leg"
},
];
// Create a back up array and iterate on the reference array, filtering the actual dicitionary array with `ppl == current reference`
const backupArr = [];
for (const ref of arrRef)
backupArr.push(...arrDicts.filter(x => x.ppl == ref))
// Now backupArr is ordered as you want to
console.log(backupArr)
请考虑到这种排序数组的方式将不包括字典数组中具有不在参考数组中的 ppl
属性的对象。