简体   繁体   English

将项目从数组的前面添加到数组的结尾

[英]Add items from front of array to end of array

This seems very simple. 这似乎很简单。 Not quiet sure how I am making sure a mess of it. 不安静地确定我要确保一团糟。

I have an list. 我有一个清单。 I want to take the first three items front the front and add them to the rear. 我想将前三个项目放在前面,然后将它们添加到后面。 I dont want to remove the items from the front. 我不想从前面删除项目。 I want them to be on the front and on the rear. 我希望它们在正面和背面。

So my list starts like this 所以我的清单是这样开始的

[1,2,3,4,5,6,7,8,9,10]

and after I am done with it will look like this 在我完成后,它看起来像这样

[1,2,3,4,5,6,7,8,9,10,1,2,3]

This list is stored in a <ul> 此列表存储在<ul>

Here is my code 这是我的代码

_options.slides = $.makeArray($('.slide'));
var el = [];
$.each(_options.slides.splice(0, 3), function (x, item) {
     el.push(item);
});
$('#video-triggers').clone().append(el);  //break point here

Someone of the code is probably unnecessary as I have been playing around for this for too long. 因为我为此花了很长时间,所以可能没有必要编写任何代码。 If I put a break point on the clone append line and execute the code in the console (development tools) here are the results 如果在克隆附加行上放置一个断点并在控制台(开发工具)中执行代码,则结果如下

在此处输入图片说明

EDIT 编辑

The clone and append line has my results, but the container doesnt have those results? 克隆和附加行有我的结果,但是容器没有这些结果吗?

Here are the results without the clone 这是没有克隆的结果

在此处输入图片说明

EDIT 2 编辑2

I also tried this 我也尝试过

$('#video-triggers').append(_options.slides.concat(el))

You have your order of operations and variables mixed up . 您将操作顺序和变量混合在一起。 . . you are cloning your target <ul> , instead of the <li> 's that you want to copy. 您正在克隆目标<ul> ,而不是要复制的<li> Try this . 尝试这个 。 . .

Change this: 更改此:

$('#video-triggers').clone().append(el);

. . . to this: 对此:

el.clone().appendTo($('#video-triggers'));

那是因为您克隆了它,然后将新元素添加到了克隆中(并且该克隆在您的页面上不存在)。

Use unshift(); 使用unshift(); to push items to front of array. 将项目推到数组的前面。

$.each(_options.slides.splice(0, 3), function (x, item) {
     el.push(item);
     el.unshift(item);
});
a = [1,2,3,4,5,6,7,8,9,10];
b = a.concat(a.slice(0,3));
console.log(b);
// [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3]

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM