• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

InDesign JS arrays quesion...

Participant ,
May 26, 2023 May 26, 2023

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.

TOPICS
Scripting , SDK

Views

222

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2023 May 26, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 26, 2023 May 26, 2023

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.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2023 May 26, 2023

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 )

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 26, 2023 May 26, 2023

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 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 26, 2023 May 26, 2023

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

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 26, 2023 May 26, 2023

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 )

 

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Participant ,
May 26, 2023 May 26, 2023

Copy link to clipboard

Copied

Nice!

Thanks for the posts and alternative solutions 🙂

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Guide ,
May 26, 2023 May 26, 2023

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 )

 

(^/)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
May 26, 2023 May 26, 2023

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

 

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
People's Champ ,
May 28, 2023 May 28, 2023

Copy link to clipboard

Copied

LATEST

Why on Earth is Uwe's solution weird? Would be curious.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines