Javascript 如何使用另一个数组对一个数组进行排序?

Javascript 如何使用另一个数组对一个数组进行排序?,javascript,angular,typescript,Javascript,Angular,Typescript,我正在构建一个Angular 9应用程序。 在这个应用程序中,我需要使用另一个数组对一个数组进行排序 这是第一个数组: [{ "field_key": "title", "field_type": "text", "field_label": "Title", "rows": [{ "id"

我正在构建一个Angular 9应用程序。 在这个应用程序中,我需要使用另一个数组对一个数组进行排序

这是第一个数组:

[{
    "field_key": "title",
    "field_type": "text",
    "field_label": "Title",
    "rows": [{
        "id": 267255299,
        "content": "Personal task"
    }, {
        "id": 568449863,
        "content": "Super"
    }, {
        "id": 110557130,
        "content": "Another child"
    }, {
        "id": 365047528,
        "content": "Another task"
    }, {
        "id": 383976941,
        "content": "Nice"
    }, {
        "id": 811735335,
        "content": "Arrays"
    }, {
        "id": 324862914,
        "content": "Arrays"
    }, {
        "id": 485226870,
        "content": "Yay a child"
    }, {
        "id": 276334971,
        "content": "My new task"
    }, {
        "id": 549277506,
        "content": "Douplicatee"
    }]
}, {
    "field_key": "progress",
    "field_type": "progress",
    "field_label": "Progress",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": 100
    }, {
        "id": 365047528,
        "content": 100
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": 100
    }, {
        "id": 485226870,
        "content": null
    }, {
        "id": 276334971,
        "content": 60
    }, {
        "id": 549277506,
        "content": 0
    }]
}, {
    "field_key": "ends_at_date",
    "field_type": "date",
    "field_label": "Due date",
    "rows": [{
        "id": 267255299,
        "content": null
    }, {
        "id": 568449863,
        "content": null
    }, {
        "id": 110557130,
        "content": null
    }, {
        "id": 365047528,
        "content": null
    }, {
        "id": 383976941,
        "content": null
    }, {
        "id": 811735335,
        "content": null
    }, {
        "id": 324862914,
        "content": null
    }, {
        "id": 485226870,
        "content": "2020-08-25"
    }, {
        "id": 276334971,
        "content": "2020-08-16"
    }, {
        "id": 549277506,
        "content": null
    }]
}]
这是我要用来对第一个数组进行排序的数组。第二个数组中的“key”键与第一个数组中的“field_key”值相同。因此,我想我应该能够使用这些值进行排序。

[{
      key: 'title',
      label: 'Title',
      field: 'text',
      visible: true
    }, {
      key: 'ends_at_date',
      label: 'Due date',
      field: 'date',
      visible: true
    }, {
      key: 'progress',
      label: 'Progress',
      field: 'progress',
      visible: true
    }]

我想对第一个数组进行重新排序,使其与基于键/字段\键值的最后一个数组的顺序相同。现在,第一个数组的顺序是:Title,Progress,Ends,at,date,但第二个数组(正确的数组)的顺序是Title,Ends,at,date,Progress。

根据您的评论,您的要求现在更清楚了。请试一试

const array1 = [{
    "field_key": "title",
    "field_type": "text",
    "field_label": "Title",
}, {
    "field_key": "progress",
    "field_type": "progress",
    "field_label": "Progress",
}, {
    "field_key": "ends_at_date",
    "field_type": "date",
    "field_label": "Due date",
}];

const array2 = [{
      key: 'title',
      label: 'Title',
      field: 'text',
      visible: true
    }, {
      key: 'ends_at_date',
      label: 'Due date',
      field: 'date',
      visible: true
    }, {
      key: 'progress',
      label: 'Progress',
      field: 'progress',
      visible: true
    }];

const sorted = array1.sort((left, right) => array2.findIndex(sortKey => sortKey.key === left.field_key) - array2.findIndex(sortKey => sortKey.key === right.field_key));

console.log(sorted);

如果您不想进行就地排序,我想您可以只获取不同键的索引,然后设置结果数组的索引,如下例所示

常量数组1=[{
“字段\ U键”:“标题”,
“字段类型”:“文本”,
“字段标签”:“标题”,
“行”:[{
“id”:267255299,
“内容”:“个人任务”
}, {
“id”:568449863,
“内容”:“超级”
}, {
“id”:110557130,
“内容”:“另一个孩子”
}, {
“id”:365047528,
“内容”:“另一项任务”
}, {
“id”:383976941,
“内容”:“不错”
}, {
“id”:811735335,
“内容”:“数组”
}, {
“id”:324862914,
“内容”:“数组”
}, {
“id”:485226870,
“内容”:“是个孩子”
}, {
“id”:276334971,
“内容”:“我的新任务”
}, {
“id”:549277506,
“内容”:“双重性”
}]
}, {
“字段_键”:“进度”,
“字段类型”:“进度”,
“字段标签”:“进度”,
“行”:[{
“id”:267255299,
“内容”:空
}, {
“id”:568449863,
“内容”:空
}, {
“id”:110557130,
“内容”:100
}, {
“id”:365047528,
“内容”:100
}, {
“id”:383976941,
“内容”:空
}, {
“id”:811735335,
“内容”:空
}, {
“id”:324862914,
“内容”:100
}, {
“id”:485226870,
“内容”:空
}, {
“id”:276334971,
“内容”:60
}, {
“id”:549277506,
“内容”:0
}]
}, {
“字段密钥”:“在日期结束”,
“字段类型”:“日期”,
“字段标签”:“截止日期”,
“行”:[{
“id”:267255299,
“内容”:空
}, {
“id”:568449863,
“内容”:空
}, {
“id”:110557130,
“内容”:空
}, {
“id”:365047528,
“内容”:空
}, {
“id”:383976941,
“内容”:空
}, {
“id”:811735335,
“内容”:空
}, {
“id”:324862914,
“内容”:空
}, {
“id”:485226870,
“内容”:“2020-08-25”
}, {
“id”:276334971,
“内容”:“2020-08-16”
}, {
“id”:549277506,
“内容”:空
}]
}];
常数数组2=[{
关键字:“标题”,
标签:“标题”,
字段:“文本”,
可见:正确
}, {
关键字:“在日期结束”,
标签:“截止日期”,
字段:“日期”,
可见:正确
}, {
关键:“进展”,
标签:“进度”,
字段:“进度”,
可见:正确
}];
函数排序外观(array1,array2){
const dict=Object.assign({},…array2.map((v,i)=>({[v.key]:i}));
常量结果=数组(数组1.长度);
for(数组1的let值){
结果[dict[value.field_key]]=值;
}
返回结果;
}

console.log(sortbyaappearance(array1,array2))你真的是指排序还是过滤?我真的没有得到您想要在这里排序的内容。我想对第一个数组重新排序,使其与基于键/字段\键值的最后一个数组的顺序相同。现在,第一个数组的顺序是:Title,Progress,Ends,at,date,但第二个数组(正确的数组)的顺序是Title,Ends,at,Progress。所以,所有的边数据都无关紧要,您只希望第一个数组的索引与第二个数组中的出现顺序相匹配?