我目前正在开发一个程序来对一组并行数组进行排序。该程序假设使用myArray.sort()
方法/函数对第一个数组进行排序。当程序打印出它们时,第二个数组值应与第一个数组排序保持一致。我认为我非常接近解决它但不能完全围绕它。sortArrays()
函数是唯一需要工作的逻辑,其余的工作完美。任何帮助将不胜感激。
Javascript
var employeeName = [];
var employeeSal = [];
var tmpArrayName = [];
var tmpArraySal = [];
var arraySize = 0;
function getInfo() {
arraySize = parseInt(prompt("How many employee records will you be adding today?"));
while (isNaN(arraySize)) {
arraySize = parseInt(prompt("Error: please enter a valid positive number for question (How many employee records will you be adding today?)"));
}
for (var l = 0; l < arraySize; l++) {
employeeName[l] = prompt("Please enter the employee's last name and first name (ex: Wayne John)");
employeeSal[l] = parseInt(prompt("Please enter the employee's yearly salary (ex: 62000)"));
while (isNaN(employeeSal[l])) {
employeeSal[l] = parseInt(prompt("Please enter the employee's yearly salary (ex: 62000)"));
}
}
tmpArrayName = employeeName;
tmpArraySal = employeeSal;
employeeName.sort();
sortArrays();
printResult();
}
function sortArrays() {
var index = 0;
for (var i = 0; i < employeeName.length; i++) {
index = employeeName.indexOf(tmpArrayName[i]);
employeeSal[index] = tmpArraySal[i];
}
}
function printResult() {
for (var k = 0; k < employeeName.length; k++) {
document.write('' + employeeName[k] + ' ' + employeeSal[k] + '<br/>');
}
}
HTML
<body onload="getInfo()">
<div id ="content">
</div>
</body>
答案 0 :(得分:1)
问题在于您没有复制数组,而是引用原始数组。要制作副本,请使用切片:
$('#zoomPicture').elevateZoom({
scrollZoom : true,
zoomWindowFadeIn: 250,
zoomWindowFadeOut: 500,
responsive: true,
easing: true,
easingDuration: 50,
borderSize: 1,
zoomWindowWidth: 400,
zoomWindowHeight: 400,
});
$("#zoomPicture").bind("click", function(e) {
console.log(e.pageX);
console.log(e.pageY);
}); // -> undefined
应该是
tmpArrayName = employeeName;
tmpArraySal = employeeSal;
答案 1 :(得分:0)
好的,这是最终解决方案。 @ caleb-an你的解决方案效果很好,但我通过使用for循环将tmpArrays值设置为原始数组来解决它。
//this code works fine but can not use
//tmpArrayName = employeeName.slice(0);
//tmpArraySal = employeeSal.slice(0);
//this code does the same exact thing and works
for (var e = 0; e < employeeName.length; e++)
{
tmpArrayName[e] = employeeName[e];
tmpArraySal[e] = employeeSal[e];
}