js sort()方法改变原数组了怎么办

let arr = nums.sort((a, b) => a-b)
let arr1 = nums.map(item => item*item)
let target = arr1.sort((a, b) => a-b)  // arr1被修改
let arr = nums.sort((a, b) => a-b)
let arr1 = nums.map(item => item*item)
let target = arr1.slice().sort((a, b) => a-b)  // arr1不被修改
posted @ 2022-02-26 09:50  叶子玉  阅读(405)  评论(0编辑  收藏  举报