首页 > 解决方案 > 无法将属性添加到循环中的对象数组

问题描述

好吧,伙计们,我在这里完全被难住了。简要概述一下我在这里所做的事情,我正在使用 Mongoose 从 MongoDB 中提取文档。这是从 mongoose 查询返回的对象数组的示例:

[
  {
    "_id": "5fdd2f26724b95367bb08557",
    "buyer": "5f976504231cd73151dcdc28",
    "project": "5fa30ee658eba14ca75eb69c",
    "createdBy": "5fdbd0f42d8fb4348f3136fd",
    "createdAt": "2020-12-18T22:37:26.170Z",
    "updatedAt": "2020-12-18T22:37:26.170Z",
    "__v": 0
  },
  {
    "_id": "5fdd3216724b95367bb0856f",
    "buyer": "5f976504231cd73151dcdc28",
    "project": "5fa30ee658eba14ca75eb69c",
    "createdBy": "5fdbd0f42d8fb4348f3136fd",
    "createdAt": "2020-12-18T22:49:58.320Z",
    "updatedAt": "2020-12-18T22:49:58.320Z",
    "__v": 0
  }
]

拉出后,我将使用 Fisher-Yates 样式函数对它们进行随机化:

async function randomizer(array){
    let currentIndex = array.length, temporaryValue, randomIndex;
      
    // While there remain elements to shuffle...
    while (0 !== currentIndex) {
  
      // Pick a remaining element...
      randomIndex = Math.floor(Math.random() * currentIndex);
      currentIndex -= 1;
  
      // And swap it with the current element.
      temporaryValue = array[currentIndex];
      array[currentIndex] = array[randomIndex];
      array[randomIndex] = temporaryValue;
    }
  
    return array;
}

然后,我采用该随机数组并使用 for 循环遍历它:

for(let i = 0; i < reservations.length; i++){
      reservations[i].randomizedOrder = i; // Store randomized order in array
}

但是数组中的对象仍然像这样出现,没有添加 randomOrder 字段:

  {
    "_id": "5fdd3216724b95367bb0856f",
    "buyer": "5f976504231cd73151dcdc28",
    "project": "5fa30ee658eba14ca75eb69c",
    "createdBy": "5fdbd0f42d8fb4348f3136fd",
    "createdAt": "2020-12-18T22:49:58.320Z",
    "updatedAt": "2020-12-18T22:49:58.320Z",
    "__v": 0
  }

这是按顺序排列的:

      const options = {
        query: { project: "5fa30ee658eba14ca75eb69c" },
        populate: [],
        pagination: { limit: 0, skip: 0 },
        sort: {},
      };
      
      const reservations = await this.dbService.read('Reservation', options);
      const arr = reservations.data;

      function randomizer(array) {
        let currentIndex = array.length,
            temporaryValue, randomIndex;
    
        // While there remain elements to shuffle...
        while (0 !== currentIndex) {
    
            // Pick a remaining element...
            randomIndex = Math.floor(Math.random() * currentIndex);
            currentIndex -= 1;
    
            // And swap it with the current element.
            temporaryValue = array[currentIndex];
            array[currentIndex] = array[randomIndex];
            array[randomIndex] = temporaryValue;
        }
    
        return array;
      }

      const shuffledReservations = randomizer(arr);

      for(let i = 0; i < shuffledReservations.length; i++){
        arr[i].shuffledReservations = i; // Store randomized order in array
      }

我在 for 循环中添加了一个 console.log() 行,并且 for 循环肯定正在运行,因为它正在吐出 console.logs 但它只是没有将 randomOrder 属性添加到保留中。我在这里做错了什么?

标签: javascriptnode.jsarrays

解决方案


你不需要await关键字来调用随机化器!并从函数声明中去掉 async关键字。

如果您没想到这个输出并且您在实施时遇到任何问题,请发表评论!

let arr = [{
        "_id": "5fdd2f26724b95367bb08557",
        "buyer": "5f976504231cd73151dcdc28",
        "project": "5fa30ee658eba14ca75eb69c",
        "createdBy": "5fdbd0f42d8fb4348f3136fd",
        "createdAt": "2020-12-18T22:37:26.170Z",
        "updatedAt": "2020-12-18T22:37:26.170Z",
        "__v": 0
    },
    {
        "_id": "5fdd3216724b95367bb0856f",
        "buyer": "5f976504231cd73151dcdc28",
        "project": "5fa30ee658eba14ca75eb69c",
        "createdBy": "5fdbd0f42d8fb4348f3136fd",
        "createdAt": "2020-12-18T22:49:58.320Z",
        "updatedAt": "2020-12-18T22:49:58.320Z",
        "__v": 0
    }
];
function randomizer(array) {
    let currentIndex = array.length,
        temporaryValue, randomIndex;

    // While there remain elements to shuffle...
    while (0 !== currentIndex) {

        // Pick a remaining element...
        randomIndex = Math.floor(Math.random() * currentIndex);
        currentIndex -= 1;

        // And swap it with the current element.
        temporaryValue = array[currentIndex];
        array[currentIndex] = array[randomIndex];
        array[randomIndex] = temporaryValue;
    }
    console.log(array)
    return array;
}

for(let i = 0; i < arr.length; i++){
    arr[i].randomizedOrder = i; // Store randomized order in array
}

const shuffledReservations = randomizer(arr);


推荐阅读