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

If Array includes String

New Here ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Hi all.

 

I'm trying to write a script that will get all the existing Custom Text variables in a document and add or update from a list as appropriate.

I've got the read from file / add new / update fuctions working ok, but I'm stuck on checking if the custom text variable currently exists.

 

// get existing varibales
var currentVars = [];
currentVars = get_variables();

// Throws Error : Error Code# 24: currentVars.includes is not a function
if (currentVars.includes("MyVariable")) {
        // run update function
	$.writeln('MyVariable exists');
}
else {
        // run add new function
	$.writeln('MyVariable does not exist');
}

function get_variables () {
		var arr = [];
		var v = app.activeDocument.textVariables;
		for (var i = 0; i < v.length; i++) {
            if (v[i].variableType == VariableTypes.customTextType) {
 				arr.push (v[i].name);
			}
		}
		return arr;
	};

 

The debugger is showing the currentVars as an array and I can see all the custom text variable names are indexed items eg. 0: MyVariable

 

Any idaeas where I'm going wrong with this?

TOPICS
Scripting

Views

660

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 2 Correct answers

Community Expert , Apr 28, 2022 Apr 28, 2022

Array.includes is not part of Extendscript. You need to roll your own includes/contains function. Something like: 

 

Array.prototype.includes = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) { return true; }
    }
    return false;
}

Votes

Translate

Translate
Community Expert , Apr 28, 2022 Apr 28, 2022

An example of the third approach of using join method and a regex is mentioned below

var a = ["hello1", "sample", "hello", "array"]
var searchStr = "hello"
var r = new RegExp("(,|^)" + searchStr + "(,|$)")
alert(r.test(a.join()))

P.S.:- Code edited, the regex made more robust

-Manan

Votes

Translate

Translate
Community Expert ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

Array.includes is not part of Extendscript. You need to roll your own includes/contains function. Something like: 

 

Array.prototype.includes = function(obj) {
    var i = this.length;
    while (i--) {
        if (this[i] === obj) { return true; }
    }
    return false;
}

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

Copy link to clipboard

Copied

Hi @emittiv,

As the error suggests array does not have any includes method hence you can't use it. There are multiple ways you can handle it.

  • Instead of using an array you could use an object to store your values. Something like obj[v[i].name] = true. Then in order to check whether the value exists in the object or not, you just need to check if(obj["MyVariable"])
  • If you want to use array then the simplest way would be to run a loop over every element and then check if it is equal to the value being searched and if it is then break the loop else if you hit the end of the array then the element is not found.
  • You could also join all the array values together into a string using the join method and then use a regex to check if the value exists in the array or not.

-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
Community Expert ,
Apr 28, 2022 Apr 28, 2022

Copy link to clipboard

Copied

LATEST

An example of the third approach of using join method and a regex is mentioned below

var a = ["hello1", "sample", "hello", "array"]
var searchStr = "hello"
var r = new RegExp("(,|^)" + searchStr + "(,|$)")
alert(r.test(a.join()))

P.S.:- Code edited, the regex made more robust

-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