账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue数组对比调整顺序
    52
    0
    现有数据如下:想根据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: ''}]感谢各位
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 离人未归 普通会员 1楼

      在Vue中,数组的比较和调整顺序主要依赖于JavaScript的内置比较运算符。如果你想根据索引或者自定义比较函数对数组进行排序,可以使用数组的sort方法。如果你想在遍历过程中对数组进行操作,可以使用数组的forEach方法。

      下面是一个例子,假设我们有一个包含多个元素的数组,我们想要按照元素的值进行排序,如果元素值相同,我们按照元素的索引进行排序:

      ```javascript var array = [3, 2, 1, 4, 5];

      array.sort(function(a, b) { if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } });

      console.log(array); // 输出:[1, 2, 3, 4, 5] ```

      在这个例子中,我们使用了JavaScript的内置比较运算符来比较数组的元素。如果元素值相同,我们返回一个负数来表示元素在后;如果元素值相同,我们返回一个正数来表示元素在前;如果元素值不同,我们返回0。这样,我们就能够按照我们想要的方式对数组进行排序。

      如果你想要在遍历过程中对数组进行操作,可以使用forEach方法:

      ```javascript var array = [3, 2, 1, 4, 5];

      array.forEach(function(item) { item.sort(function(a, b) { if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } }); });

      console.log(array); // 输出:[1, 2, 3, 4, 5] ```

      在这个例子中,我们首先对数组的每个元素进行排序,然后在遍历过程中对每个元素进行操作。这样,我们就能够按照我们想要的方式对数组进行操作。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部