[英](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.