尽管我根本没有碰过它,但为什么我的数组会颠倒过来?

发布于 2025-01-10 16:39:33 字数 3017 浏览 0 评论 0原文

我正在构建一个网站,并且有一个路由器,但是问题不是路由器,而是其中的计算,我正在制作一个数组 tomato ,在将我想要的内容放入其中后,所有值都是好的,但是在我渲染页面之前,数组的值显然会颠倒过来?我什至没有触及其他代码上的值,如下所示:

router.get('/reportsNested', function(req,res,next){
  Question.find({
    $expr: {
      $gt: [
        {
          "$size": "$reportsNested"
        },
        0
      ]
    }
  }).sort({reportsNested: -1}).limit(20).exec(function(err,results){
    let tomato = [];
    for(let i =0; i< results.length; i++){
      for(let j =0; j < results[i].reportsNested.length; j++){
        tomato.push(results[i].reportsNested[j]);
      }
    }
    console.log(tomato)
    for(let i =0; i< results.length; i++){
      let reserveranswers = results[i].answers
      let answersArr = []
      for(let j =0; j < results[i].answers.length; j++){
        answersArr.push(results[i].answers[j])
      }
      answersArr = answersArr.sort((a,b) => (a.likes[0]-a.dislikes[0] < b.likes[0]-b.dislikes[0]) ? 1 : ((a.likes[0]-a.dislikes[0] > b.likes[0]-b.dislikes[0]) ? -1 : 0));
      results[i].answers = answersArr;
      let reserveranswersText = [];
      for(let j =0; j < results[i].answers.length; j++){
        reserveranswersText.push(results[i].answers[j].text)
      }
      for(let j =0; j<results[i].reportsNested.length; j++){
        for(let h = 0; h<reserveranswers.length; h++){
          results[i].reportsNested[j].answerIndex = reserveranswersText.indexOf(results[i].reportsNested[j].answerText);
        }
      }
    }
    console.log(tomato)
    res.render('reports', {type: 'nested', user: req.user, reports: results, reporttoremove: tomato})
  })
})

当我第一次在控制台记录番茄数组时,它输出:

 [
  {
    username: 'SweetWhite',
    answerIndex: 2,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">me an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemon</h2>`
  },
  {
    username: 'SweetWhite',
    answerIndex: 0,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">do I become an amongusdo I become an amongusdo I become an amongusdo I become an amongus</h2>`
  }
]

第二次输出番茄时,它输出这个! :

[
  {
    username: 'SweetWhite',
    answerIndex: 0,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">me an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemon</h2>`
  },
  {
    username: 'SweetWhite',
    answerIndex: 2,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">do I become an amongusdo I become an amongusdo I become an amongusdo I become an amongus</h2>`
  }
]

我已经阅读这段代码几个小时了,我不明白为什么数组是相反的! 任何帮助将不胜感激!

I am building a website and I have a router, however the problem is not the router, but the calculations inside it, I am making an array tomato and after putting what I want in it all the values are okay, however right before I render a page the values of the array apparently get reversed? I am not even touching the values on the other code, here it is:

router.get('/reportsNested', function(req,res,next){
  Question.find({
    $expr: {
      $gt: [
        {
          "$size": "$reportsNested"
        },
        0
      ]
    }
  }).sort({reportsNested: -1}).limit(20).exec(function(err,results){
    let tomato = [];
    for(let i =0; i< results.length; i++){
      for(let j =0; j < results[i].reportsNested.length; j++){
        tomato.push(results[i].reportsNested[j]);
      }
    }
    console.log(tomato)
    for(let i =0; i< results.length; i++){
      let reserveranswers = results[i].answers
      let answersArr = []
      for(let j =0; j < results[i].answers.length; j++){
        answersArr.push(results[i].answers[j])
      }
      answersArr = answersArr.sort((a,b) => (a.likes[0]-a.dislikes[0] < b.likes[0]-b.dislikes[0]) ? 1 : ((a.likes[0]-a.dislikes[0] > b.likes[0]-b.dislikes[0]) ? -1 : 0));
      results[i].answers = answersArr;
      let reserveranswersText = [];
      for(let j =0; j < results[i].answers.length; j++){
        reserveranswersText.push(results[i].answers[j].text)
      }
      for(let j =0; j<results[i].reportsNested.length; j++){
        for(let h = 0; h<reserveranswers.length; h++){
          results[i].reportsNested[j].answerIndex = reserveranswersText.indexOf(results[i].reportsNested[j].answerText);
        }
      }
    }
    console.log(tomato)
    res.render('reports', {type: 'nested', user: req.user, reports: results, reporttoremove: tomato})
  })
})

when I am console logging tomato array for the first time it outputs:

 [
  {
    username: 'SweetWhite',
    answerIndex: 2,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">me an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemon</h2>`
  },
  {
    username: 'SweetWhite',
    answerIndex: 0,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">do I become an amongusdo I become an amongusdo I become an amongusdo I become an amongus</h2>`
  }
]

And for the second time it outputs tomato it outputs this! :

[
  {
    username: 'SweetWhite',
    answerIndex: 0,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">me an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemonme an lemon lemon</h2>`
  },
  {
    username: 'SweetWhite',
    answerIndex: 2,
    answerText: `<h2 class="makewrap" style="flex-wrap: wrap; overflow-wrap: break-word; font-family: 'Times New Roman';">do I become an amongusdo I become an amongusdo I become an amongusdo I become an amongus</h2>`
  }
]

I have been reading this code for hours and I don't understand why the array is reversed!
Any help would be very appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温柔嚣张 2025-01-17 16:39:33

番茄反转的原因是排序更新了引用。

尝试这样做。

const tomato = [];
for (let i = 0; i < results.length; i++) {
  for (let j = 0; j < results[i].reportsNested.length; j++) {
    tomato.push({ ...results[i].reportsNested[j] });
  }
}

我在我的机器上尝试过,它可以工作。

The reason for the tomato getting reversed is sort updating the reference.

Try doing this.

const tomato = [];
for (let i = 0; i < results.length; i++) {
  for (let j = 0; j < results[i].reportsNested.length; j++) {
    tomato.push({ ...results[i].reportsNested[j] });
  }
}

I tried in my machine and it works.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文