Copy link to clipboard
Copied
Hi everyone,
Just a quick question about arrays...
Is there a quick way to pull out all the even numbered elements separately and the same for the odd numbered ones?
Or, is it a question of looping through the array with an increment of 2 ?
Thanks in advance.
Copy link to clipboard
Copied
I suppose whatever you do would involve a loop mostly, unless you join the array values into a string and do some Grep magic. Is running a loop troublesome in your case?
-Manan
Copy link to clipboard
Copied
Hi Manan,
Thanks for the reply. Interesting idea regarding using GREP.
I haven't tried the loop method yet, just thought I'd check in advance that I'm not missing a nice arrays method.
The array I'm dealing with will only have a few hundred items in it, nothing too big.
Thanks again.
Copy link to clipboard
Copied
Hi @deckarduk ,
let's see:
"Looping the array with increments of 2 would be one solution."
Exactly this.
Examples:
var a =
[
0 ,
1 ,
2 ,
3 ,
4,
5
];
var resultArrayStartWithZero = [];
for( var n=0; n<a.length; n=n+2 )
{
resultArrayStartWithZero[resultArrayStartWithZero.length++] = n ;
};
// Result: 0,2,4
resultArrayStartWithOne = [];
for( var n=1; n<a.length; n=n+2 )
{
resultArrayStartWithOne[resultArrayStartWithOne.length++] = n ;
};
// Result: 1,3,5
Regards,
Uwe Laubender
( Adobe Community Expert )
Copy link to clipboard
Copied
Hi Uwe,
Thanks for the post.
You've echoed the method I was going to use. A loop with an increment of 2 and changing the start point i.e. n=0 or n=1.
Thank you again for the help 🙂
Copy link to clipboard
Copied
Hi Uwe,
Weird approach! …
var a =
[
"a",
"b",
"c",
"d",
"e",
"f",
"g"
];
var resultArrayStartWithZero = [];
for( var n=0; n<a.length; n=n+2 ) resultArrayStartWithZero.push(a[n]);
alert( "1/ " + resultArrayStartWithZero )
var resultArrayStartWithOne = [];
for( var n=1; n<a.length; n=n+2 ) resultArrayStartWithOne.push(a[n]);
alert( "2/ " + resultArrayStartWithOne )
(^/) The Jedi
Copy link to clipboard
Copied
Variation:
var a =
[
"a",
"b",
"c",
"d",
"e",
"f",
"g"
];
for ( var i = a.length-1; i >= 0; i=i-2 ) a.splice(i,1);
alert( "1/ " + a )
var a =
[
"a",
"b",
"c",
"d",
"e",
"f",
"g"
];
for ( var i = a.length-2; i >= 0; i=i-2 ) a.splice(i,1);
alert( "2/ " + a )
(^/)
Copy link to clipboard
Copied
Nice!
Thanks for the posts and alternative solutions 🙂
Copy link to clipboard
Copied
Other variant! =D
var X =
[
"a",
"b",
"c",
"d",
"e",
"f",
"g"
];
var Y = [], Z = [];
for ( var i = 0; i < X.length; i=i+2 ) Y.push(X.slice(i,i+1));
for ( var i = 1; i < X.length; i=i+2 ) Z.push(X.slice(i,i+1));
alert( "X = " + X + "\rY = " + Y + "\rZ = " + Z )
(^/)
Copy link to clipboard
Copied
Thanks for the posts and alternative solutions
There’s also the modulus operator:
var a = ["a", "b", 3, 4, "dog", "cat", 10, 20];
var even = [];
var odd = [];
for (var i = 0; i < a.length; i++){
if (i % 2 == 1) {
even.push(a[i])
} else {
odd.push(a[i])
}
};
$.writeln(even)
//returns b,4,cat,20
$.writeln(odd)
//returns a,3,dog,10
Copy link to clipboard
Copied
Why on Earth is Uwe's solution weird? Would be curious.