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

Save "position" array in page item note for reuse?

LEGEND ,
Mar 27, 2022 Mar 27, 2022

Copy link to clipboard

Copied

I'd like to save the x,y position array of an object for reuse later. I can save the "position" to the note

rcraighead_0-1648431122156.png [49.8303615172063,11.4548352296533]

but when accessed by the code below it does not act like an array. What am I doing wrong?

var coords = (selection[0].note);
var myArray = coords.split(',');
selection[0].position = [myArray[0], myArray[1]];
 
The goal is to reuse an element in another document and position it the same as it was in the original document.
TOPICS
Scripting

Views

348

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

correct answers 3 Correct answers

Community Expert , Mar 27, 2022 Mar 27, 2022

Hi @rcraighead,

The note is stored as a sting.

The split function return numbers as a string. Try to use conversion as below

var coords = (selection[0].note);
var myArray = coords.split(',');
selection[0].position = [Number(myArray[0]), Number(myArray[1])];

 

Also, you can save JSON object if required.

{
  x: 49.8303615172063, 
  y: 11.4548352296533
}

 

Votes

Translate

Translate
Enthusiast , Mar 27, 2022 Mar 27, 2022
for (var i = 0; i < myArray.length; i++) {
    alert(typeof myArray[i]); // "string"
}
try {
    selection[0].position = ["44", "55"]; // This is what you're doing:
} catch (err) {
    // Which results in an error because you can't apply strings to numeric values:
    alert(err); // "Error: Point value expected"
}

As Charu points out you'd have to convert them to typeof Numbers before being able to apply. Just as an extra trick, you can do this in all sorts of different ways in native JS:

~~"3.14"
...

Votes

Translate

Translate
Guide , Mar 28, 2022 Mar 28, 2022

Saving you using note, split and converting, you could save position in your own defined array property.  E.g.

 

selection[0].position1 = selection[0].position;
// do something
selection[0].position = selection[0].position1;

 

 

Votes

Translate

Translate
Adobe
Community Expert ,
Mar 27, 2022 Mar 27, 2022

Copy link to clipboard

Copied

Hi @rcraighead,

The note is stored as a sting.

The split function return numbers as a string. Try to use conversion as below

var coords = (selection[0].note);
var myArray = coords.split(',');
selection[0].position = [Number(myArray[0]), Number(myArray[1])];

 

Also, you can save JSON object if required.

{
  x: 49.8303615172063, 
  y: 11.4548352296533
}

 

Best regards

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
Enthusiast ,
Mar 27, 2022 Mar 27, 2022

Copy link to clipboard

Copied

for (var i = 0; i < myArray.length; i++) {
    alert(typeof myArray[i]); // "string"
}
try {
    selection[0].position = ["44", "55"]; // This is what you're doing:
} catch (err) {
    // Which results in an error because you can't apply strings to numeric values:
    alert(err); // "Error: Point value expected"
}

As Charu points out you'd have to convert them to typeof Numbers before being able to apply. Just as an extra trick, you can do this in all sorts of different ways in native JS:

~~"3.14" // 3
+"3.14" // 3.14 > This is what I normally always use, but it can hurt readability
"3.14"*1 // 3.14 > This can be the fastest, most performant way to convert from String > Number
Number("3.14") // 3.14 > This is the most reliable for largest integers
Math.round("3.14") // 3 > Math.floor, ceil, and round accept either string or number

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 ,
Mar 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

Saving you using note, split and converting, you could save position in your own defined array property.  E.g.

 

selection[0].position1 = selection[0].position;
// do something
selection[0].position = selection[0].position1;

 

 

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
LEGEND ,
Mar 28, 2022 Mar 28, 2022

Copy link to clipboard

Copied

LATEST

Thank you all for the great 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