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.jpg', 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.jpg', 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.jpg', lastModified: 1631256394638, lastModifiedDate: Fri Sep 10 2021 14:46:34 GMT+0800 (中国标准时间), webkitRelativePath: ''},
{uid: 1638867869536, name: 'orange.jpg', 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: ''}
]
感谢各位
回答:
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));
以上是 vue数组对比调整顺序 的全部内容, 来源链接: utcz.com/p/936628.html