将属性添加到 for 循环中的最后一项
Add a property to last item in for loop
for (i = 0; i < len; i++) {
dStep = links[i].getAttribute("data-step"),
dIntro = links[i].getAttribute("data-intro"),
linkObj = {
element: "#step" + dStep,
intro: dIntro,
position: "right"
};
obj.steps.push(linkObj);
如何将位置:"左"添加到循环中的最后一项?
if (i == len-1) {
linkObj.position = "left";
}
// You should almost certainly be using the var keyword
for (var i = 0; i < len; i++) {
var dStep = links[i].getAttribute("data-step"),
dIntro = links[i].getAttribute("data-intro"),
linkObj = {
element: "#step" + dStep,
intro: dIntro,
position: "right"
};
obj.steps.push(linkObj);
}
// Take advantage of the fact that JavaScript doesn't have block scoping
linkObj.position = "left";
for (i = 0; i < len; i++) {
dStep = links[i].getAttribute("data-step"),
dIntro = links[i].getAttribute("data-intro"),
linkObj = {
element: "#step" + dStep,
intro: dIntro,
position: "right"
};
// try this
if (i === len - 1) {
linkObj.position = 'left';
}
obj.steps.push(linkObj);
由于它是您推送到数组中的最后一个项目,因此您也可以在 for
循环后添加/更改数组中最后一项的属性:
for (i = 0; i < len; i++) {
dStep = links[i].getAttribute("data-step"),
dIntro = links[i].getAttribute("data-intro"),
linkObj = {
element: "#step" + dStep,
intro: dIntro,
position: "right"
};
obj.steps.push(linkObj);
}
// add this line of code after the for loop
obj.steps[obj.steps.length - 1].position = "left";
相关文章:
- 如何从对象数组中获取最后一项
- 为什么只有最后一项显示,而不是全部显示
- 删除最后一项jquery
- 为什么我的“for”循环只获取 html 中的最后一项
- 我无法将下拉列表的默认值设置为最后一项
- 流星批量插入仅插入最后一项
- 我怎么能知道元素是最后一项
- 如何删除WinJS组绑定列表的最后一项
- 使用handlers.js模板以数组中的最后一项为条件
- empty().append()追加循环中的最后一项
- jQuery只附加最后一项
- 将属性添加到 for 循环中的最后一项
- 如何按排序键顺序获取对象中的最后一项
- for 循环中的 addEventListener 始终添加到最后一项
- 从数组中删除最后一项
- CasperJS只从我的for循环中多次发布最后一项
- 在 JavaScript 中处理数组中的最后一项
- 胡子.js带有数组,最后一项除外
- 通过jQuery在最后一项之后插入
- JavaScript for 循环只返回数组中的最后一项