简体   繁体   English

在一条线上推动和取消移动是行不通的。 我究竟做错了什么?

[英]pushing and unshifting in one line doesn't work. what am i doing wrong?

I'm getting an array of ints from an API denoting days.我从表示天数的 API 获得了一系列整数。

[0, 7, 30, 356]

I wrote a small function that appends the moment -generated end-date (calculating days from now) but that's not important.我写了一个小的 function 附加了moment生成的结束日期(从现在开始计算天数),但这并不重要。 The important part is that the second line of my ternary.重要的部分是我的三元的第二行。

On 0 , I don't want to calculate the end-date, but just "no limit" as label and that entry as the last in the array0上,我不想计算结束日期,而只是“无限制”作为 label 并且该条目作为数组中的最后一个

wanted result:想要的结果:

[{
  code: 7
  label: 'one week'
},
{
  code: 30,
  label: 'one month'
},
{
  code: 356,
  label: 'one year'
},
{
  code: 0,
  label: 'no limit
}]

I wanted to get real fancy and was already kind of proud about the elegant one-liner, but it just doesn't seem to work.我想得到真正的幻想,并且已经对优雅的单线感到自豪,但它似乎不起作用。 It doesn't remove the first entry.它不会删除第一个条目。 entering it in devtools by itself returns the object literal though, so that part seems to be correct.在 devtools 中输入它本身会返回 object 文字,所以这部分似乎是正确的。

for (let i = 0, x = lengths.length; i < x; i++) {
  lengths[i]
    ? lengths[i] = //transform the entry here
    : lengths.push(lengths.shift() && {code: lengths[i], label: 'No limit'})
}
return lengths

Can anyone point out what exactly I am did wrong?谁能指出我到底做错了什么? I want to understand我想了解

clarification:澄清:

I want to remove 0 from the first array position, transform and add it to the last postition at the same time我想从第一个数组position中去掉0,同时变换添加到最后一个位置

edit编辑

thanks to an answer here, i noticed that i used unshift instead of shift to remove the first element.. which of course didn't work感谢这里的回答,我注意到我使用unshift而不是shift来删除第一个元素..这当然没有用

lengths.unshift() returns value , so it adding same value, not the {code: lengths[i], label: 'No limit'} lengths.unshift()返回value ,因此它添加相同的值,而不是{code: lengths[i], label: 'No limit'}

You can use splice:您可以使用拼接:

lengths.splice(lengths.length-1, 1, {code: lengths[i], label: 'No limit'})

Here remove 1, an element at position lengths.length-1 and add object .此处remove 1,即position lengths.length-1处的元素并添加object

With some ugly code you can do something like this, i am using , operator to shift() and adjust the variables due to changes index because of mutation使用一些丑陋的代码,你可以做这样的事情,我正在使用,运算符来shift()并调整变量,因为由于突变而改变索引

 let lengths = [0, 30, 60, 90] for (let i = 0, x = lengths.length; i < x; i++) { lengths[i]? lengths[i] = {code: lengths[i], label: "dummy"}: (lengths.push({code: lengths[i], label: 'No limit'}), lengths.shift(), i--, x--) } console.log(lengths)


Close to your original code,接近您的原始代码,

  1. ! need before shift as the return value of shift is 0 ( in our case ) so to make && operator return object we need to make it truthy value需要在 shift 之前,因为 shift 的返回值是0 (在我们的例子中)所以要让&&运算符返回 object 我们需要使它成为真值
  2. Need to adjust indexes else we miss out elements需要调整索引,否则我们会错过元素

 let lengths = [0, 30, 60, 90] for (let i = 0, x = lengths.length; i < x; i++) { lengths[i]? lengths[i] = {code: lengths[i], label: "dummy"}: (lengths.push(.lengths:shift() && {code, 0: label, 'No limit'}), i--.x--) } console.log(lengths)

PS:- 1st point can be avoided by using || PS:- 第一点可以通过使用||来避免operator as nicely pointed out in nina's answer运营商在妮娜的回答中很好地指出

You need a logical OR ||你需要一个逻辑或|| instead of a logical AND && , because shift returns zero and this is falsy.而不是逻辑 AND && ,因为shift返回零,这是错误的。 The following part in never evaluated this zero is pushed.以下部分从未评估过这个零被推送。

lengths.push(lengths.shift() || { code: lengths[i], label: 'No limit' })
//                           ^^

You could iterate from the end and shift at the end where item is left.您可以从末尾迭代并在 item 所在的末尾移动。

 var lengths = [0, 7, 30, 356], i = lengths.length; while (i--) { if (lengths[i]) lengths[i] = { code: lengths[i], label: '...' }; else { lengths.shift(); lengths.push({ code: 0, label: 'no limit' }); } } console.log(lengths);

A short approach is to shift and push before you change the values.一种简短的方法是在更改值之前移动和推动。

 var lengths = [0, 7, 30, 356]; lengths.push(lengths.shift()); console.log(lengths);

暂无
暂无

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

相关问题 将obj-c类传递给javascript不起作用。 我究竟做错了什么? - Passing obj-c class to javascript doesn't work. What am I doing wrong? JSF 2.2表单上的tagmanager.js不起作用。 我究竟做错了什么? - tagmanager.js on JSF 2.2 form doesn't work. What am I doing wrong? 带有偏移的边栏不起作用。 我怎么了 - Sidebar with offset doesn't work. What do I wrong? 通过 javascript 更改图像源似乎不起作用。 我究竟做错了什么? - Change image source through javascript does not seem to work. What am I doing wrong? getElementsByClassName在IE6上不起作用。 我究竟做错了什么? - getElementsByClassName doesn't work on IE6. What am I doing wrong? 我正在猜测数字项目,但我的 if else 似乎无法正常工作。 我究竟做错了什么? - im doing a guess the number project but my if else doesn't seem to work properly. what am i doing wrong? AJAX更新面板不起作用。 请帮助我找出我在哪里错...? - AJAX Update Panel Doesn't Work. Please help me finding where am I wrong…? 我正在尝试使用此 JavaScript 行滚动 div 容器,但它不起作用。 为什么? - I'am trying to scroll a div container with this JavaScript line, but it doesn't work. Why? 删除节点时,pruneRoots 函数在下面的树算法中返回 undefined。 无法让它工作。 我究竟做错了什么? - pruneRoots function returning undefined in the below tree algorithm when removing a node. Unable to make it work. What am I doing wrong? 我用Javascript函数和验证编写了HTML代码。但是验证不起作用,我在做什么错 - I Wrote This HTML Code with Javascript Functions and Validations..but the Validation doesn't work, what am i doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM