# Strange JS behaviour

**URL:** <https://forums.fusetools.com/t/strange-js-behaviour/6545>\
**Category:** Bug Reports\
**Created:** [June 26, 2016, 6:32pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545 "2016-06-26T18:32:33Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![lg1](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lg1](https://forums.fusetools.com/u/lg1)\
**Post date:** [June 26, 2016, 6:32pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/1 "2016-06-26T18:32:33Z")

</div>

In this examples Im trying to find an item in the ‘list’ that contains a nested Observable with id:1. good\_value enumeration works as expected but bad\_value is not, also when debugging the x.nested.count breakpoint is never stops. Do I missing something, I want to use Observable.count insted of .toArray workaround

```auto
<App>
    <StackPanel>
        <JavaScript>
            var Observable = require("FuseJS/Observable");

            var list = Observable({id:100, nested: Observable({id:1, text: "11"}, {id:2, text: "2"})}, {id:100, nested: Observable({id:1, text: "11"}, {id:2, text: "2"})});

            var good_value = list.where(function(x){
                var array = x.nested.toArray();
                for (v in array)
                    if (array[v].id == 1)
                        return true;
                return false;
            }).count();

            var bad_value = list.where(function(x){
                var c = x.nested.count(function(y) {
                    debugger;
                    return y.id == 1;
                });
                debugger;
                return c > 0;
            }).count();

            module.exports =
            {
                good_value: good_value,
                bad_value: bad_value
            }; </JavaScript>

        <Text Value="{good_value}" />
        <Text Value="{bad_value}" />
    </StackPanel>
</App>

```

---

<div class="post-metadata">

**Author:** ![Olle\_Fredriksson](https://avatars.discourse-cdn.com/v4/letter/o/e95f7d/32.png) [@Olle\_Fredriksson](https://forums.fusetools.com/u/Olle_Fredriksson)\
**Post date:** [June 28, 2016, 8:59am UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/2 "2016-06-28T08:59:52Z")

</div>

Hey lg, and thanks for your report!

Don’t forget that `count` returns an observable number, so `return c > 0` is not meaningful there. Does it work if you change that to e.g. `return c.map(function(d) { return d > 0; })`?

---

<div class="post-metadata">

**Author:** ![lg1](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lg1](https://forums.fusetools.com/u/lg1)\
**Post date:** [June 28, 2016, 5:52pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/3 "2016-06-28T17:52:21Z")

</div>

Now it works, and the break-point beeing hit, thanks for the explanation 😉

---

<div class="post-metadata">

**Author:** ![lg1](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lg1](https://forums.fusetools.com/u/lg1)\
**Post date:** [July 3, 2016, 7:24pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/4 "2016-07-03T19:24:47Z")

</div>

Didnt want to create another thread, but whats the difference between .expand() and manually adding items to the list. The result of this code is not the same, see test3

```auto
<App>
    <StackPanel>
        <JavaScript>
            var Observable = require("FuseJS/Observable");

            var data = [1,2,3];

            var test1 = Observable({id:'ok', list: Observable(data).expand()});
            var test2 = Observable({id:'ok', list: Observable()});
            for (i in data)
                test2.value.list.add(data[i]);

            var test3 = test1.map(function(x) { console.log(x.list); return JSON.stringify(x.list.toArray()); })
            var test4 = test2.map(function(x) { console.log(x.list); return JSON.stringify(x.list.toArray()); })

            module.exports = {
                test1: test1,
                test2: test2,

                test3: test3,
                test4: test4
            }
        </JavaScript>
        <Text Value="test1" />
        <Each Items="{test1.list}">
            <Text Value="{}" />
        </Each>

        <Text Value="test2" />
        <Each Items="{test2.list}">
            <Text Value="{}" />
        </Each>

        <Text Value="test3" />
        <Text Value="{test3}" />

        <Text Value="test4" />
        <Text Value="{test4}" />
    </StackPanel>
</App>

```

---

<div class="post-metadata">

**Author:** ![Anders\_Bondehagen](https://yyz1.discourse-cdn.com/flex035/user_avatar/forums.fusetools.com/anders_bondehagen/32/98_2.png) [@Anders\_Bondehagen](https://forums.fusetools.com/u/Anders_Bondehagen)\
**Post date:** [July 4, 2016, 1:39pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/5 "2016-07-04T13:39:32Z")

</div>

If you open a new thread it’s easier for other people that have the same question 🙂 I can see that there is some strange results here. I need to get back to you. I have filed an issue about this.

---

<div class="post-metadata">

**Author:** ![lg1](https://avatars.discourse-cdn.com/v4/letter/l/cdc98d/32.png) [@lg1](https://forums.fusetools.com/u/lg1)\
**Post date:** [July 4, 2016, 1:42pm UTC](https://forums.fusetools.com/t/strange-js-behaviour/6545/6 "2016-07-04T13:42:07Z")

</div>

Thanks!
