[英]Javascript slice isn't giving me correct array length values
Why does it say length 1 instead of 4? 为什么说长度为1而不是4? The following is what I'm trying to push and slice.
以下是我要推送和切片的内容。 I try and append items.image_urls and slice them into 5 each.
我尝试添加items.image_urls并将它们切成5个。
items.image_urls
is my dictionary array. items.image_urls
是我的字典数组。
var final_push = []
final_push.push(items.image_urls.splice(0,5))
console.log(final_push.length)## gives me 1...?
var index = 0
final_push.forEach(function(results){
index++ ##this gives me one. I would need 1,2,3,4,5,1,2,3,4,5. Somehting along that.
}
items.image_urls looks like this: It's an iteration of arrays with image urls. items.image_urls看起来像这样:它是带有图像URL的数组的迭代。
That seems to be a nested array. 那似乎是一个嵌套数组。 So if you would access index 0, and then work on that array like below it will probably work:
因此,如果您要访问索引0,然后像下面那样在该数组上工作,它可能会起作用:
console.log(final_push[0].length); //should print 4
In your example items.image_urls.splice(0,5)
returns an array of items removed from items.image_urls
. 在您的示例中,
items.image_urls.splice(0,5)
返回从items.image_urls
删除的项目数组。 When you call final_push.push(items.image_urls.splice(0,5));
当您调用
final_push.push(items.image_urls.splice(0,5));
, this whole array is pushed as one item to the final_push
array, so it now looks like [["url1", "url2", "url3", "url4", "url5"]]
(2-dimensional array). ,
final_push
整个数组作为一项推入final_push
数组,因此现在看起来像[["url1", "url2", "url3", "url4", "url5"]]
(二维数组)。 You can access this whole array by calling final_push[some_index]
. 您可以通过调用
final_push[some_index]
来访问整个数组。
But what you want instead is to add every element of items.image_urls.splice(0,5)
to the final_push
. 但是,您想要的是将
items.image_urls.splice(0,5)
每个元素添加到final_push
。 You can use a spread operator to achieve this: 您可以使用传播运算符来实现此目的:
final_push.push(...items.image_urls.splice(0,5));
Spread syntax allows an iterable such as an array expression or string to be expanded in places where zero or more arguments (for function calls) or elements (for array literals) are expected
扩展语法允许将迭代式(例如数组表达式或字符串)扩展到期望零个或多个参数(用于函数调用)或元素(用于数组文字)的位置
This is exactly our case, because push() expects one or more arguments: 这正是我们的情况,因为push()需要一个或多个参数:
arr.push(element1[, ...[, elementN]])
And here is an example: 这是一个示例:
let items = { image_urls: ["url1", "url2", "url3", "url4", "url5", "url6", "url7", "url8", "url9", "url10"] }; let final_push = []; final_push.push(...items.image_urls.splice(0,5)); console.log(final_push.length); console.log(JSON.stringify(final_push)); console.log(JSON.stringify(items.image_urls));
Note: do not confuse Array.prototype.slice() with Array.prototype.splice() - the first one returns a shallow copy of a portion of an array into a new array object while the second changes the contents of an array by removing existing elements and/or adding new elements and returns an array containing the deleted elements . 注意:请勿将Array.prototype.slice()与Array.prototype.splice()混淆-第一个将数组一部分的浅表副本返回到新的数组对象中,而第二个则通过删除来更改数组的内容现有元素和/或添加新元素,并返回包含已删除元素的数组 。
The author is mixing up splice
and slice
. 作者正在
splice
和slice
。 Probably a typo :) 可能是拼写错误:)
You start at the beginning (0) and then delete 5 items. 您从开头(0)开始,然后删除5个项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.