TypeError:无法设置属性&#39;#<object>&#39;在python风格的javascript中交换数组元素时未定义的

时间:2017-11-24 15:03:38

标签: javascript arrays

我正在尝试在javascript中实现选择排序。

&#13;
&#13;
let array = require('./../../utility/array').array;
const printJustValues = require('./../../utility/printArray');

const selectionsort = (array) => {    
    let minimum;
    for(let i = 0;i < array.length; i++) {
        minimum = i;
        for(let j = i; j < array.length; j++) {            
            if(array[j].nested.value < array[minimum].nested.value) {                
                minimum = j;
            }
        }
        
        [ array[minimum], array[i] ] = [ array[i], array[minimum] ];
        
        // let temp = array[i];
        // array[i] = array[minimum];
        // array[minimum] = temp;
    }
    return array;
}

let result = selectionsort(array);
printJustValues(result);
&#13;
&#13;
&#13;

在交换元素时,当我尝试使用python样式时,它会抛出错误,如:

&#13;
&#13;
TypeError: Cannot set property '#<Object>' of undefined
    at selectionsort (/Users/mihirima/Documents/github/Algorithms-and-Data-Structures/algorithms/sorting/selectionsort/selectionsort.js:16:32)
    at Object.<anonymous> (/Users/mihirima/Documents/github/Algorithms-and-Data-Structures/algorithms/sorting/selectionsort/selectionsort.js:25:14)
    at Module._compile (module.js:635:30)
    at Object.Module._extensions..js (module.js:646:10)
    at Module.load (module.js:554:32)
    at tryModuleLoad (module.js:497:12)
    at Function.Module._load (module.js:489:3)
    at Function.Module.runMain (module.js:676:10)
    at startup (bootstrap_node.js:187:16)
    at bootstrap_node.js:608:3
&#13;
&#13;
&#13;

任何人都可以解释为什么会这样吗?感谢。

0 个答案:

没有答案