简体   繁体   English

(JavaScript,Redux)为什么我的嵌套数组在返回时被删除,但是当我不返回时却没有?

[英](JavaScript, Redux) Why are my nested array's getting deleted when returning, however when I don't return it doesn't?

What I'm trying to do is take an array I am getting from an api, and cutting it down to smaller arrays of 4 elements maximum in each of the smaller arrays and store them in my redux store. What I'm trying to do is take an array I am getting from an api, and cutting it down to smaller arrays of 4 elements maximum in each of the smaller arrays and store them in my redux store.

Here is how I'm calling the function:这是我如何调用 function:

const newArray = res.data.Search
let list = paginate(newArray)
console.log(list)
dispatch({ type: FETCH_SEARCH_SUCCESS, payload: list });

This is what I'm sending in the array:这是我在数组中发送的内容:

[{Title: "The Avengers", Year: "2012"}, 
{Title: "Avengers: Infinity War", Year: "2018"}, 
{Title: "Avengers: Endgame", Year: "2019"}, 
{Title: "Avengers: Age of Ultron", Year: "2015"}, 
{Title: "The Avengers", Year: "1998"}, 
{Title: "The Avengers: Earth's Mightiest Heroes", Year: "2010–2012"},  
{Title: "Ultimate Avengers II", Year: "2006"}, 
{Title: "The Avengers", Year: "1961–1969"}, 
{Title: "Avengers Assemble", Year: "2012–2019"},
{Title: "Ultimate Avengers: The Movie", Year: "2006"}]

This is the code block for this problem:这是这个问题的代码块:

const paginate = (arr) => {
  const mr = 4;
  let pn = 0;
  let results = []

  // calculate max number of pages user can go through (if its only 4 results or less it will just return the array as is)
  if(arr.length < mr){
    return arr
  }else {
    const maxPages = Math.ceil(arr.length/mr)
    // will make a nested array from every 4 indexes of arr, and put it inside of the results array
    for(let i = 0; i < maxPages; i++){
      results[i] = arr.slice(mr*pn, mr*pn+mr)
      pn+=1
    }
    console.log(results)
    //return results
  }
}

This is what is coming back in my console.logs when I dont use return results当我不使用return results时,这就是我的 console.logs 中返回的内容在此处输入图像描述

And this is whats coming back when I do use the return results这就是我使用return results 在此处输入图像描述

I don't think the issue here lies withing the paginate function.我认为这里的问题不在于分页 function。 I am guessing this is a rendering issue when you return the results you might be triggering the function to be called again with different argument maybe.我猜这是一个渲染问题,当您返回结果时,您可能会触发 function 以不同的参数再次调用。 I would suggest checking out what argument you are passing each time and what is the output.我建议检查您每次传递的参数以及 output 是什么。 Could you provide some more info as to what are the arguments to recreate the issue?您能否提供更多关于 arguments 的信息来重新创建问题?

 const paginate = (arr) => { const mr = 4; let pn = 0; let results = [] // calculate max number of pages user can go through (if its only 4 results or less it will just return the array as is) if(arr.length < mr){ return arr }else { const maxPages = Math.ceil(arr.length/mr) // will make a nested array from every 4 indexes of arr, and put it inside of the results array for(let i = 0; i < maxPages; i++){ results[i] = arr.slice(mr*pn, mr*pn+mr) pn+=1 } console.log(results) return results } } var x = paginate([1,2,3,4,5,6])

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Javascript计算排列-当我不复制数组时,为什么我的代码返回不需要的解决方案? - Javascript computing permutations - why is my code returning unwanted solution when I don't make a copy of the array? 为什么在测试不包含 0 的数组中是否存在 0 时,javascript 的“in”运算符返回 true? - Why does javascript's “in” operator return true when testing if 0 exists in an array that doesn't contain 0? 为什么当我尝试将函数的返回值与字符串连接时我的代码不起作用? - why my code doesn't work when I am trying to concatenate a function's return value with a string? 我不知道为什么我的JavaScript函数Flip()不返回任何东西。 我希望它返回是正面还是反面 - I don't know why my function Flip() in JavaScript isn't returning anything. I want it to return if it is heads or tails 为什么即使我没有输入名称,验证也无法在javascript中的所有代码上起作用,但它什么也没有提醒 - Why the validation doesn't work at all the code in javascript even when I don't write the name it doesn't alert anything 为什么我的JavaScript不能将数组返回到AppleScript列表? - Why doesn't my JavaScript return the array to an AppleScript list? 为什么当我调用具有返回值的函数时,我的onclick事件不起作用? - Why when i call a function with returning value, my onclick event doesn't work? 在 javascript 中,为什么在 promise 捕获时 await 不返回错误? - In javascript, why doesn't await return an error when a promise catches? 我知道为什么我会收到意外的 { 令牌,但当我的 php 需要将数据集合发送到 javascript 时,我不知道如何修复它 - I know why I am getting unexpected { token but I don't know how to fix it when my php needs to send collection of datas to javascript 为什么我的JavaScript不返回网址? - Why doesn't my javascript return a url?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM