Ronald
Ronald

Reputation: 76

Can't add a property to an array of objects in a loop

Alright guys, I am totally stumped here. A little overview of what I am doing here, I am pulling documents from MongoDB using Mongoose. Here's an example of the array of objects returned from the mongoose query:

[
  {
    "_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
  }
]

Once pulled, I am randomizing them with a Fisher-Yates style function:

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;
}

I then take that randomized array and loop through it with a for loop:

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

But the objects in the array are still coming out like this with no added randomizedOrder field:

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

Here it is in order:

      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
      }

I added a console.log() line in the for loop and the for loop is definitely running as it's spitting out console.logs but it's just not adding the randomizedOrder property to reservation. What am I doing wrong here?

Upvotes: 0

Views: 170

Answers (1)

Re9iNee
Re9iNee

Reputation: 620

you didnt need await keyword for calling randomizer! and strip out async keyword from function declaration as well.

if you didn't expect this output and you have any problems implementing leave a comment!

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);

Upvotes: 1

Related Questions