NodeJS,看发帖的点就知道现在有多无奈了

financings是一个对象数组,我想对他进行打乱,并将结果数组压入plan数组,但我遇到的问题是:所有的item,都是一致的,我本想将return后面的小数用math.random代替,打乱的结果是也一致,但确实打乱了一次,我想知道如何让这个financings真正打乱了5次,这段代码是在一个nodejs 的app.get()里
img

下面是我用了洗牌算法写的,还是一样

app.post('/api/plot/assetSearch', (req, res) => {
    var plan=[]
    function shuffle(array) {
        var currentIndex = array.length,  randomIndex;
      
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
      
          // Pick a remaining element...
          randomIndex = Math.floor(Math.random() * currentIndex);
          currentIndex--;
      
          // And swap it with the current element.
          [array[currentIndex], array[randomIndex]] = [
            array[randomIndex], array[currentIndex]];
        }
      
        return array;
      }
                var a = [{name:1},{name:2},{name:3},{name:4},{name:5}]
        var b =[]
            for(var i=0;i<=2;i++){
             b=shuffle(a)
            console.log("b",b)
        }
             for(var i=0;i<=2;i++){
              shuffle(a)
              console.log("b",a)
        }
        console.log("打乱后的5个备选方案为",plan)
}

因为sort()方法和你的洗牌算法都是对原数组对象自身进行排序(打乱),返回值也是原数组对象自身的引用。不会产生新的数组对象。
所以你所有的item都是同一个数组对象的引用。压入plan中的都是同一个数组对象。
应该是在用sort()进行排序(打乱)前或者排序(打乱)后,把数组克隆一份,让压入plan中的都是不同的数组对象

var plan=[]
var a = [{name:1},{name:2},{name:3},{name:4},{name:5}]
for(var i=0;i<5;i++){
    var b = a.slice(0); //把a数组克隆一份
    b.sort(function(){return Math.random()>0.5?1:-1;});
    plan.push(b);
}
console.log("打乱后的5个备选方案为",plan)

img