如何在另一个数组中复制一个数组(JavaScript)

时间:2019-01-26 00:17:52

标签: javascript

我的JS函数遇到问题... 在此函数中,我尝试将localState的值保留在其他两个数组中(localState0,localState1),并且在尝试重置localState值的循环结束时,这部分localState[cellI][cellJ] = turn(localStates, val);都无法正常工作localStatelocalState0发生了变化,我不知道为什么!

var minMax = function(val){
    var localState = state.slice()
    var states = []
    var evals = []

    for(k =0; k< 16; k++){
        var  localState0 = []
        localState0 = localState.slice()
        var cellI = converter(k)[0]
        var cellJ = converter(k)[1]
        if (localStates[cellI][cellJ] == ""){
            localState[cellI][cellJ] = turn(localStates, val);
            var localState1 = []
            localState1 = localState.slice()
            for(var w=0; w<16; w++){
                var cellII = converter(w)[0]
                var cellJJ = converter(w)[1]
                if (localState[cellII][cellJJ] == ""){
                    localState[cellII][cellJJ] = turn(localStates, val);
                    states.push(localState);
                    evals.push(eval(localState, val))
                    localState = localState1
                }
            }
        }
        localState = localState0
    }
return states
}

* turn函数返回类似"X" or "O"

的字符串

1 个答案:

答案 0 :(得分:0)

您应该使用localState0 = JSON.parse(JSON.stringify(localState))代替localState0 = localState.slice()