我目前正在为大学做一些课程。我正在尝试将旧数组的单个值复制到新数组,然后将旧数组值设置为 0。显然,如果我只是将值分配给新数组,然后更改旧数组值,它将覆盖新数组数组也是。
我不允许使用函数 splice()。
这是我的代码:
function rankedScores(web, pattern) {
var v = urlScores(web, pattern);
var sorted = [];
var maxIndex = 0;
while (sorted.length < v.length) {
for (var i = 0; i < v.length; i += 1) {
if (v[i].score > v[maxIndex].score) {
maxIndex = i
}
}
sorted[sorted.length] = v[maxIndex];
v[maxIndex].score = 0;
maxIndex = 0;
}
alert(sorted[0].url + ' ' + sorted[0].score)
alert(sorted[1].url + ' ' + sorted[1].score)
alert(sorted[2].url + ' ' + sorted[2].score)
}
如果我这样做,它会返回正确的 URL 值,但所有分值为 0。
关于如何阻止数组指向同一内存位置的任何想法?
我试过使用 for 循环,因为我看到这做了一个浅拷贝,但它没有用
干杯。