admin管理员组文章数量:1410712
I'm trying to access every other/second element in an array, such as 2, 4, 6, 8 etc.. but can't get it to work. Any suggestions?
This is my function:
function tall(nummer) {
var tall = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
if (nummer == 5) {
for(var i = 0; i <= tall.length; i++) {
if(!i % 2 == 0) {
const partall = a => a.filter(e => +e == e && e%2);
document.getElementById("tall").innerHTML = partall(tall);
}
}
}
I'm trying to access every other/second element in an array, such as 2, 4, 6, 8 etc.. but can't get it to work. Any suggestions?
This is my function:
function tall(nummer) {
var tall = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
if (nummer == 5) {
for(var i = 0; i <= tall.length; i++) {
if(!i % 2 == 0) {
const partall = a => a.filter(e => +e == e && e%2);
document.getElementById("tall").innerHTML = partall(tall);
}
}
}
Share
Improve this question
edited Oct 9, 2018 at 14:52
ckhedda
asked Oct 9, 2018 at 14:50
ckheddackhedda
731 silver badge7 bronze badges
5
-
you getting any errors?
for(var i = 0; i <= tall.length; i++)
should lead to a nullpointerexception as the index ends 1 before the length – elPolloLoco Commented Oct 9, 2018 at 14:53 - @elPolloLoco nope – ckhedda Commented Oct 9, 2018 at 14:54
- wait, where are you accessing tall anyways? – elPolloLoco Commented Oct 9, 2018 at 14:55
- its a div in html, if thats what you are asking – ckhedda Commented Oct 9, 2018 at 14:55
- Why is there a loop of the array and inside the loop filtering the array? On every iteration, you just remove what you stick in the innerHTML? – epascarello Commented Oct 9, 2018 at 15:05
2 Answers
Reset to default 6You can use bitwise AND &
to discern even-odd indices and filter.
tall.filter((_,i) => i&1)
var tall = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15],
result = tall.filter((_,i) => i&1);
console.log( result );
There is an order of operations issue with the line
if(!i % 2 == 0) {
Where the !
has higher precedence than the %
. So the value of i is being coerced to a boolean (false for any non-zero number) and then that boolean is coerced to a 0
for the modulus operation (the result of 0 % 2
always being 0 so the statement always evaluates to true). If you wanted to check if i
was even, you would want to write that
if(i % 2 == 0)// i divided by two has no remainder
however, you are doing this in a loop, and in the truthy case you are applying a filter to the entire array. The result of that filter operation won't change per iteration of the loop, so there's really no point to doing it more than once.
const partall = a => a.filter(e => +e == e && e%2);
+e
is a unary operator to coerce the value in e
to a number (which it already is) but then you loosely pare a number to e && e%2
which will result in a boolean (true if the number is not evenly divisible by two). If you want all even numbers, you can rewrite this
const partall = a => a.filter(e => !(e%2));
The following should work. I've removed the loop entirely as it serves no real function here.
function tall(nummer) {
var tall = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15];
if (nummer == 5) {
const partall = a => a.filter(e => !(e%2));
document.getElementById("tall").innerHTML = partall(tall);
}
}
tall(5);
<div id='tall'></div>
本文标签: javascriptEvery other element in an arrayStack Overflow
版权声明:本文标题:javascript - Every other element in an array - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744871796a2629671.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论