Expand my Community achievements bar.

Applications for the 2024 Adobe Target Community Mentorship Program are open! Click to the right to learn more about participating as either an Aspirant, to professionally level up with a new Certification, or as a Mentor, to share your Adobe Target expertise and inspire through your leadership! Submit your application today.

Retrieving array values within a profile

Avatar

Level 5

Hi

I'm attempting to retrieve specific values from an array I have stored within a profile.  The usual method I would use to retrieve these via Javascript doesn't appear to be working, so not sure if I've missed something obvious or not?!

I have created an array with 2 dimensions, which, from looking at the mbox trace, seems to look as I would expect it to look, see below:

1681320_pastedImage_2.png

The above array contains 4 2 dimensional values.  If I copy this local profile to a variable, e.g.

var testArray = [];

var testArray = user.getLocal('Debug_AA.Init_2');

..and apply the '.length' i.e. testArray.length, it returns 4, which I would expect.  I have also been able to push values to this, again, verifying for me this is an array.

When I attempt to retrieve a value from this, e.g. testArray[0][0], like I would normally do, it doesn't work, when I save the profile I get the following. 

1681321_pastedImage_3.png

Maybe there's a specific way in which to get these values within the profiles which is different from regular javascript?! I'd be grateful for any help.

Many thanks

Dan

5 Replies

Avatar

Level 10

Hi Dan,

Could you attach the full script?

Avatar

Level 5

Hi Andrey

I've included the beginning of the script, see below.  To summarise, I'm retrieving the contents of local profile 'Product_Affinity' and putting this in the variable 'product_affinity' if it exists.  I'm then attempting to put the value located in index [0][0] of this 2 dimension array into the variable 'array_pick'.  This is the line that creates the error.

// *** Profile Script beginning

var product_affinity = [];

var prod_display_index;

var prod_display;

var prod_type_pos;

var add_prod;

var add_prod_value;

var prod_affinity_val;

var prod_type;

var pts=[];

var new_session;

var i;

//var push_array;

if (user.getLocal('Product_Affinity')) {

user.setLocal('Debug_AA.Init_1','prod_aff from profile: ' + user.getLocal('Product_Affinity')); // for purposes of debugging

product_affinity = user.getLocal('Product_Affinity'); //

//user.setLocal('Debug_AA.Init_2','prod_aff from var: ' + product_affinity); // for purposes of debugging

}else if (!user.getLocal('Product_Affinity')) {

user.setLocal('Debug_AA.Init_2','Profile doesnt exist'); // for purposes of debugging

}

user.setLocal('Debug_AA.Init_3', product_affinity); // for purposes of debugging to prove product_affinity exists

// *** Populate product_affinity var array, used for testing purposes only

//var push_array = new Array(99, 5);

//product_affinity.push(push_array);

//user.setLocal('Product_Affinity',product_affinity);

  // user.setLocal('Debug_AA.Init_5: prod affinity push', product_affinity); // for purposes of debugging to prove product_affinity exists at this point

// get array value

var array_pick;

array_pick = product_affinity[0][0]; // this line breaks the script, commenting this out allows script to run!

user.setLocal('Debug_AA.Init_6:[0][0]',array_pick);

// *** Profile Script ends

Many thanks


Dan

Avatar

Level 10

Dan,

Add if ( product_affinity && product_affinity[0] && product_affinity[0][0] ) before array_pick = product_affinity[0][0];

This will help prevent a JS error if the array element does not exist.

Let me know if that solves the issue.

Avatar

Level 5

Hi Andrey

Unfortunately this doesn't solve it, for some reason when I try to use those 2 index positions in the product_affinity variable, e.g. product_affinity[x][y] the code errors.  I don't know whether the javascript within the profiles is capable of taking 2 dimensions, seems odd I'm able to count the number and push, but seem unable to retrieve these individual values within it.

Thanks

Dan

Avatar

Level 10

Hi Dan,

It seems to me there may be something with the code logic rather than with the limited support of multi-dimensional arrays... If you are sure in the code logic, try to replace the array with an object.