Creative Communities of the World Forums

The peer to peer support community for media production professionals.

Activity Forums Adobe After Effects Expressions addition of 2 vectors (javascript)

  • addition of 2 vectors (javascript)

    Posted by Xavier Gomez on August 18, 2012 at 3:58 pm

    Hello,

    is there a javascript method to add two arrays of numbers (something like Array.add(array)) or does one have to always write a loop ?

    The problem i encountered is the following:
    if V1 and V2 are arrays of numbers of the same length, to make their sum one can simply write: sum = V1+V2, but if the common length is >1000, then sum is an array of length 1000 (it took me ages to realize this…lost so much time), the remaining entries are not calculated. For instance, if POLO is an array of length 1300 whose entries are all equal to 1, then all entries of POLO+POLO are equal to 2, but POLO+POLO has length …1000, not 1300.

    If instead of writing sum = V1+V2 one explicitely writes:

    for (k=0; k < V1.length; k++)
    {
    sum[k] = V1[k]+V2[k];
    }

    then sum has the correct length.
    Really weird, and very well hidden feature. Is there a reason why it is so ?

    Xavier Gomez replied 14 years ago 2 Members · 2 Replies
  • 2 Replies
  • Dan Ebberts

    August 18, 2012 at 5:12 pm

    As far as I know, JavaScript doesn’t include a function for adding arrays. I would guess Adobe added it to the expression language mainly to make it easier to manipulate small, property-sized arrays, although I wasn’t aware there was a size limit.

    I haven’t tried this, but the only thing I can think of is that if the array lengths are greater than 1000, you could break them into pieces using JavaScript’s Array.slice(), add the pieces using Adobe’s + operator, and use Array.concat() to put the pieces together. Ugly, but it should work.

    Dan

  • Xavier Gomez

    August 18, 2012 at 9:12 pm

    Thank you for your answer.
    I opted for a loop and componentwise addition.
    But… I still find this feature very curious!

    Xavier.

We use anonymous cookies to give you the best experience we can.
Our Privacy policy | GDPR Policy