vue数组对比调整顺序

现有数据如下:想根据arr1的uid来排arr2的顺序

let arr1 = [
    {
        "attachment": "blob:http://localhost:8096/46c7bf55-e7a0-443a-8a62-ae5e89e4a8a5",
        "number": 0,
        "uid": 1638867875084,
        "id": ""
    },
    {
        "attachment": "blob:http://localhost:8096/873fbc7e-ac7c-4f02-8931-33be6828e001",
        "number": 1,
        "uid": 1638867869536,
        "id": ""
    },
    {
        "attachment": "blob:http://localhost:8096/83600690-2dfb-47fe-89ba-2ba33b9cf0d5",
        "number": 2,
        "uid": 1638867872121,
        "id": ""
    },
    {
        "attachment": "blob:http://localhost:8096/7020cf17-984e-4750-93eb-0b4178dc6b3e",
        "number": 3,
        "uid": 1638867882077,
        "id": ""
    }
]
let arr2 = [
 {uid: 1638867869536, name: 'orange.png', lastModified: 1631256394639, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''}
 {uid: 1638867872121, name: 'pro_map.gif', lastModified: 1631256394639, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''}
 {uid: 1638867875084, name: 'avatar-3.png', lastModified: 1631256394638, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''}
 {uid: 1638867882077, name: 'qcode.jpg', lastModified: 1631256394640, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''}
]

想要的数据格式:

let arr2 = [
{uid: 1638867875084, name: 'avatar-3.png', lastModified: 1631256394638, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''},
 {uid: 1638867869536, name: 'orange.png', lastModified: 1631256394639, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''},
 {uid: 1638867872121, name: 'pro_map.gif', lastModified: 1631256394639, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''},
 
 {uid: 1638867882077, name: 'qcode.jpg', lastModified: 1631256394640, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''}
]

感谢各位

阅读 2.4k
2 个回答
arr1.map(item=>arr2.find(item2=>item.uid===item2.uid)).filter(Boolean)
function sort(a, b) {
    loop: for (var i = 0; i < a.length; ++i) {
        for (var j = 0; j < b.length; ++j) {
            if (b[j].uid === a[i].uid && i !== j) {
                var tmp = b[i];
                b[i] = b[j];
                b[j] = tmp;
                continue loop;
            }
        }
    }
    return b;
}
console.dir(sort(arr1, arr2));
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题