Javascript Can';t向循环中的对象数组添加属性
好了,伙计们,我完全被难住了。对我在这里所做的工作进行一点概述,我使用Mongoose从MongoDB中提取文档。以下是mongoose查询返回的对象数组示例:Javascript Can';t向循环中的对象数组添加属性,javascript,node.js,arrays,Javascript,Node.js,Arrays,好了,伙计们,我完全被难住了。对我在这里所做的工作进行一点概述,我使用Mongoose从MongoDB中提取文档。以下是mongoose查询返回的对象数组示例: [ { "_id": "5fdd2f26724b95367bb08557", "buyer": "5f976504231cd73151dcdc28", "project": "5fa30ee658eb
[
{
"_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
}
按顺序排列如下:
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
}
const选项={
查询:{项目:“5fa30ee658eba14ca75eb69c”},
填充:[],
分页:{限制:0,跳过:0},
排序:{},
};
const reservations=wait this.dbService.read('Reservation',options);
const arr=reservations.data;
函数随机化器(数组){
让currentIndex=array.length,
时间值,随机指数;
//虽然还有一些元素需要洗牌。。。
而(0!==currentIndex){
//选择剩余的元素。。。
randomIndex=Math.floor(Math.random()*currentIndex);
currentIndex-=1;
//并将其与当前元素交换。
临时值=数组[currentIndex];
数组[currentIndex]=数组[randomIndex];
数组[randomIndex]=临时值;
}
返回数组;
}
常数shuffledReservations=随机化器(arr);
for(设i=0;i
我在for循环中添加了一个console.log()行,而for循环在抛出console.log时肯定在运行,但它只是没有将randomizeOrder属性添加到reservation中。我做错了什么?调用随机发生器时不需要等待关键字!并从函数声明中去掉async关键字
如果您没有预料到此输出,并且在实现时遇到任何问题,请留下评论
让arr=[{
“_id”:“5fdd2f26724b95367bb08557”,
“买方”:“5F976504231 CD73151DC28”,
“项目”:“5fa30ee658eba14ca75eb69c”,
“createdBy”:“5fdbd0f42d8fb4348f3136fd”,
“createdAt”:“2020-12-18T22:37:26.170Z”,
“更新日期”:“2020-12-18T22:37:26.170Z”,
“_v”:0
},
{
“_id”:“5fdd3216724b95367bb0856f”,
“买方”:“5F976504231 CD73151DC28”,
“项目”:“5fa30ee658eba14ca75eb69c”,
“createdBy”:“5fdbd0f42d8fb4348f3136fd”,
“createdAt”:“2020-12-18T22:49:58.320Z”,
“更新日期”:“2020-12-18T22:49:58.320Z”,
“_v”:0
}
];
函数随机化器(数组){
让currentIndex=array.length,
时间值,随机指数;
//虽然还有一些元素需要洗牌。。。
而(0!==currentIndex){
//选择剩余的元素。。。
randomIndex=Math.floor(Math.random()*currentIndex);
currentIndex-=1;
//并将其与当前元素交换。
临时值=数组[currentIndex];
数组[currentIndex]=数组[randomIndex];
数组[randomIndex]=临时值;
}
console.log(数组)
返回数组;
}
for(设i=0;i 常数shuffledReservations=随机化器(arr)代码>为什么要使用异步函数?预期的输出是什么?我实际上是从另一个文件调用randomizer,但为了简单起见,把它放在这里。异步会导致这个问题吗?预期的输出是对象数组,每个对象都有randomizeOrder属性addedI,在文章底部用更新的示例更新了OP。从静态数组运行此函数时,效果非常好。但当我实际从Mongoose中提取数据时,它不会将“randomizeOrder”属性添加到数组中的对象中。我认为这个问题更深,可能是dbService.read()函数出了问题。我明天会再深入一点,然后再打给你。谢谢你的帮助
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
}