# Problem with .push() or for loop?!

**URL:** https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053
**Category:** Photoshop
**Tags:** javascript
**Created:** [December 19, 2021, 1:39pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053 "2021-12-19T13:39:37Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 19, 2021, 1:39pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/1 "2021-12-19T13:39:37Z")

</div>

Hello again,

I’m currently stuck with a problem with .push() or the for loop, I don’t really get it …

I’m iterating through the channels to get the name of all alpha channels with this code:

```auto
async function testfunct(){
	const app = require('photoshop').app;
	const doc = app.activeDocument;
	const alert = require('photoshop').core.showAlert;
	const constants = require('photoshop').constants;
	var channelArray = [];

	for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
	};
	console.log(channelArray);
};

```

But unfortunately nothing gets logged to the console. But if I put the console.log() in the for loop I get a result – which makes perfect sense to me:

```auto
for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
		console.log(channelArray);
	};

```

 ![Bildschirmfoto 2021-12-19 um 14.27.33](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/1/17d42e7bc88188fb58334897fb88f43486ebc076.png)  
This is exactly the result one can expect, but that’s not really what I want. So why does my original code not work? Any Ideas? Thanks!

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 19, 2021, 1:45pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/2 "2021-12-19T13:45:36Z")

</div>

Not sure what’s happening on your machine, but it works here in console at least (notice the `FULL` log)

 ![image](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/6/6aca550f9640481edb37eb8dfcc3768cfb9c41a9.png)

BTW, I don’t think you need `async` in this case

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 19, 2021, 2:19pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/3 "2021-12-19T14:19:09Z")

</div>

Thanks for testing! Hmm? Good to see it work on your machine, are you on PS 23.1? I wasn’t … I was on 23.0. I’m updating right now and will test it again later.  
Oh and you are right, async is just there because this is only a part of a bigger function.

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 19, 2021, 2:28pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/4 "2021-12-19T14:28:25Z")

</div>

One more question: are you on windows or mac? I don’t know if this is even relevant, but what is strange is that you see blue, green and red in the console. Which means you tried this on an RGB document with no alpha channels right? When I use my code on such a doc a get no result, because for some reason only alpha channels are logged on my machine, which is strange because in ExtendScript I had to explicitly get rid of all the ChannelType.COMPONENT and ChannelType.SPOTCOLOR channels …

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 19, 2021, 2:35pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/5 "2021-12-19T14:35:24Z")

</div>

Windows 10, v23.1, fresh document without any edits

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 19, 2021, 9:00pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/6 "2021-12-19T21:00:32Z")

</div>

Thanks! Unfortunately I still cannot reproduce your results. I am now on PS 23.1 (UXP Version 5.5.6), macOS 11.2.3.

With this code …

```auto
async function testfunct(){
	const app = require('photoshop').app;
	const doc = app.activeDocument;
	const alert = require('photoshop').core.showAlert;
	const constants = require('photoshop').constants;
	var channelArray = [];

	for (var c = doc.channels.length-1; c >= 0; c--) {
		channelArray.push(doc.channels[c].name);
		console.log("LOOP",channelArray);
	};
	console.log("FULL",channelArray);
};

```

… I only get this result:

 ![Bildschirmfoto 2021-12-19 um 21.58.58](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/8/892735d3d8a480656486d6c5665c5bb81dd29a05.png)

So the “FULL” log fails and the RGB channels aren’t logged too. Really weird … And with both function and async function I get this errors about “Invalid CompositeChannel”.  
Maybe one of the Adobe devs could clarify this? @kerrishotts maybe?

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 19, 2021, 9:08pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/7 "2021-12-19T21:08:10Z")

</div>

Sounds like something’s with channels. Which manifest and API?

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 19, 2021, 10:31pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/8 "2021-12-19T22:31:49Z")

</div>

API version 2, manifest version 4.

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 19, 2021, 10:34pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/9 "2021-12-19T22:34:18Z")

</div>

How many channels do you have? Because after the second channel it fails and never finishes the loop. If you add another log before push, I suspect you’d see it, but not after push

Also try `console.log(doc.channels)` before the loop

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 20, 2021, 4:53pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/10 "2021-12-20T16:53:09Z")

</div>

I had 2 alpha channels plus the 3 RGB channels (but the never get logged on my side – don’t know why).  
Here are the results from your inputs:

 ![Bildschirmfoto 2021-12-20 um 17.48.21](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/2/2664a1b159d16ee65ba66da3d8efa340921f7c32.png)

I tested it on a completely different machine (again macOS, but 10.15.7).

But what is so strange to me is that I used the same logic of this code in ExtendScript with no problems …

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 20, 2021, 5:15pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/11 "2021-12-20T17:15:18Z")

</div>

What if you try looping the channels directly? Like

```auto
var channelArray = [];

for (let channel of doc.channels) {
  channelArray.push(channel.name);
};

```

I believe logic itself is fine here. Also, you should not get a `Proxy` object IMO ([see this topic](http://forums.creativeclouddeveloper.com/t/help-me-understand-the-documentation/4059)). Maybe that’s the reason, although I don’t see why it would be.

Could you also try just logging each channel? If it logs, see what’s the difference maybe between alpha and RGB 🤔

```auto
for (let channel of doc.channels) {
  console.log(channel);
};

```

---

<div class="post-metadata">

### Author: ![AnthonyK](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AnthonyK](https://forums.creativeclouddeveloper.com/u/AnthonyK)
#### Post date: [December 20, 2021, 7:26pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/12 "2021-12-20T19:26:48Z")

</div>

Just a thought, but your error message says: “Invalid CompositeChannel”

So maybe it’s Photoshop’s “Composite” channel that is the problem. Based on some code I’ve used, the Composite channel is NOT counted in the number of channels when Photoshop keeps track of the number of channels.

As such, you might try getting the channel’s index as well as its name to see how these line up logging this all out during the for loop.

You might also look at Alchemist to look at the “numberOfChannels” property and see if that helps. I think you’ll find that an RGB image doesn’t count the Composite channel in the total number of channels.

You’re code also allows for a `doc.channels[0].name`. Would this be the “Composite” channel? If so, your code might encounter an error at this point if the Composite channel is not able to be included in the array of channel names.

This is all speculation at this point, but if you log out the channel index as well as it’s name, you might get a clearer picture of what’s happening behind the curtain.

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 20, 2021, 9:01pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/13 "2021-12-20T21:01:12Z")

</div>

Unfortunately both of your suggestions just end with this again:

 ![Bildschirmfoto 2021-12-20 um 21.44.10](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/e/e61a077d7675072e1df0a6656da150eea73beb7b.png)

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 20, 2021, 9:09pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/14 "2021-12-20T21:09:03Z")

</div>

Hi @AnthonyK , thanks for your inputs!

The number of Channels (according to Alchemist) is 5 in this and all of my other examples. Which is exactly what I expected, because – you’re totally right – the composite channel never got counted.

 ![Bildschirmfoto 2021-12-20 um 21.45.26](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/1/180e014c0d57ecd79474b4e734e74e11929d92c7.png)

But that also means that doc.channels[0].name is not the composite, see below (I tested all indexes manually here):

 ![Bildschirmfoto 2021-12-20 um 21.48.54](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/e/efe13c11acd7b7ba2f970e71fee7d789eb107adf.png)  
That probably means that 0 = R, 1 = G and 2 = B. But what I don’t get is, when there is no CompositeChannel involved, why is there an error?

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 20, 2021, 11:22pm UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/15 "2021-12-20T23:22:37Z")

</div>

As I mentioned before, could you try logging without a `name` - just a full channel - eg. `doc.channels[2]`

---

<div class="post-metadata">

### Author: ![AnthonyK](https://avatars.discourse-cdn.com/v4/letter/a/85f322/32.png) [@AnthonyK](https://forums.creativeclouddeveloper.com/u/AnthonyK)
#### Post date: [December 21, 2021, 12:32am UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/16 "2021-12-21T00:32:26Z")

</div>

Log the index instead of the name. It’s possible that the .name value for the Red, Green, and Blue channels are problematic since there names would vary by the language Photoshop is using. See if the index value can be logged successfully. Perhaps only alpha channels can have a .name value.

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 21, 2021, 7:26am UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/17 "2021-12-21T07:26:41Z")

</div>

Exactly my point. That’s why I want to see full channel data

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 21, 2021, 10:45am UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/18 "2021-12-21T10:45:03Z")

</div>

I already tried that, but as stated above I only get an error for the component channels RGB:

 ![Bildschirmfoto 2021-12-21 um 10.59.38](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/a/a2bee35921a906f20dbead44dada69091915fb6f.png)

BTW: I tried my old ExtendScript script on the same machine today and it works perfectly fine:

 ![Bildschirmfoto 2021-12-21 um 11.39.32](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/7/7020d7c257452c29dbc6d0d26f311b8a899fea6b.jpeg)  
As you can see in the alert, all 5 channels are in the array (Red, Green, Blue and both Alpha channels).  
I’m not a developer, but for me that looks like something wrong or at least different in UXP?!

---

<div class="post-metadata">

### Author: ![Dominik\_S](https://avatars.discourse-cdn.com/v4/letter/d/ed8c4c/32.png) [@Dominik\_S](https://forums.creativeclouddeveloper.com/u/Dominik_S)
#### Post date: [December 21, 2021, 10:46am UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/19 "2021-12-21T10:46:50Z")

</div>

I also tried that, but again I only get the error:

 ![Bildschirmfoto 2021-12-21 um 11.26.51](https://us1.discourse-cdn.com/flex015/uploads/xdplugins/original/2X/b/bf83ce14313358eb87b01544316cddfdb69eb5ff.png)  
Also please see my post above.

---

<div class="post-metadata">

### Author: ![Karmalakas](https://sea1.discourse-cdn.com/flex015/user_avatar/forums.creativeclouddeveloper.com/karmalakas/32/1048_2.png) [@Karmalakas](https://forums.creativeclouddeveloper.com/u/Karmalakas)
#### Post date: [December 21, 2021, 11:16am UTC](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053/20 "2021-12-21T11:16:03Z")

</div>

I still lean to theory, that it has something to do with returned `Proxy` instead of [`Channels`](https://www.adobe.io/photoshop/uxp/2022/ps_reference/classes/channels/). I believe proxies deal with references and the error mentions wrong reference 🤔

@kerrishotts, maybe some insight?

[Next page](https://forums.creativeclouddeveloper.com/t/problem-with-push-or-for-loop/4053.md?page=2)
