2

I'm coding something in extendscript for adobe after effects, which ends up being javascript.

I have an array and I would like to do a search for just the word "assemble" and return the whole jc3_RIG_008_masterLayer assemble string

var comps = ["_MAIN", "jc3_RIG_008_masterLayer assemble","jc3_RIG_008_masterLayer contact sheet", "[Z] source", "MM004 source"];

I'm not sure what the best/ most efficient way to achieve this is, but any thoughts would help.

Thanks.

  • Just to clarify: you have the array `comps`, and you want to find the element from that array that contains the word "assemble"? – Frxstrem May 12 '15 at 19:00
  • 2
    There is no performant way, it's always O(n) except if you have a predefined set of strings and you will only search for those strings, then it could be optimized by creating maps. – maraca May 12 '15 at 19:00

3 Answers3

2

@josegomezr has the right idea using a simple loop. I updated that idea to return the string that the poster is looking for.

var comps = ["_MAIN", "jc3_RIG_008_masterLayer assemble","jc3_RIG_008_masterLayer contact sheet", "[Z] source", "MM004 source"];
var compWithAssemble;
for(var i in comps){
    if(comps[i].indexOf("assemble") > -1){
        compWithAssemble = comps[i];
        break;
    }
}
// compWithAssemble has the string you are looking for.
console.log(compWithAssemble);
Mike
  • 251
  • 1
  • 6
  • According to [MDN](https://developer.mozilla.org/es/docs/Web/JavaScript/Referencia/Objetos_globales/Array/filter) `Array.filter` exist since ECMAScript standard 5.1, and according to @Frxstrem ExtendScript only accomplish ECMAScript standard 3. – josegomezr May 12 '15 at 19:22
  • 1
    I updated my answer to use a simple loop like josegomezr. Mine returns the string that the poster is looking for. – Mike May 12 '15 at 19:34
  • Just a note: If this was a big array, using `indexOf` would perform better. `search` is generally used when you want to use regular expressions. In this scenario it obviously doesn't matter though. – Mackan May 12 '15 at 19:41
  • 1
    Thanks for the heads up. I'll change it to `indexOf`, that's what I usually use anyway. – Mike May 12 '15 at 19:47
1

A normal for-loop should do the trick. This is the fastest way according to some sources. Also using indexOf() is faster than using search() according to other sources:

for (var i = 0, len = comps.length; i < len ; i++) {
   if (comps[i].indexOf('assemble') > -1) return comps[i]; //or store and break
}

var comps = ["_MAIN", "jc3_RIG_008_masterLayer assemble","jc3_RIG_008_masterLayer contact sheet", "[Z] source", "MM004 source"];

    comps.forEach(function(el) {
       if (el.indexOf('assemble') > -1) document.write('loop 1: ' + el + '<br>');
    });


    for (var i = 0, len = comps.length; i < len ; i++) {
       if (comps[i].indexOf('assemble') > -1) document.write('loop 2: ' + comps[i]);
    }
<div id="output"></div>

I'll keep this here as reference:

Something like this would work for anything with ECMAScript5 support (but according to sources, and @frxstrem, this is not available in ExtendedScript):

comps.forEach(function(el) {
   if (el.indexOf('assemble') > -1) return el;
});
Community
  • 1
  • 1
Mackan
  • 6,200
  • 2
  • 25
  • 45
  • 1
    `array.forEach` doesn't return an array, but you can break the loop early. [Source](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). To return an array of filtered elements use `array.filter` or `array.map`. – josegomezr May 12 '15 at 19:04
  • 2
    According to [this answer](http://stackoverflow.com/a/22537459), as of last year ExtendScript did only comply with the ECMAScript 3 standard. I don't think Adobe has updated ExtendScript since then. – Frxstrem May 12 '15 at 19:06
  • "_...which ends up being javascript_" I assumed it would be more up to date. It looks like ECMAScript3 + some features from 4 is all that is available. – Mackan May 12 '15 at 19:22
  • @josegomezr I'm not trying to return an array. The OP asked for a string. – Mackan May 12 '15 at 19:38
0

If it is plain JS you can do this:

var found = false;
for(var i in comps){
    if(comps[i].search("assemble") != -1){
        found = true;
        break;
    }
}
if(found){
    // your code if found
}else{
    // your code if not.
}
josegomezr
  • 888
  • 4
  • 15