[英]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 array在
0
上,我不想计算结束日期,而只是“无限制”作为 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
我想了解
I want to remove 0 from the first array position, transform and add it to the last postition at the same time我想从第一个数组position中去掉0,同时变换添加到最后一个位置
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,接近您的原始代码,
!
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 value0
(在我们的例子中)所以要让&&
运算符返回 object 我们需要使它成为真值 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.