FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

ObservableArray.reduce from tns-core-modules doesn't handle initial value correctly · Issue #6385 · NativeScript/NativeScript · GitHub

ObservableArray.reduce from tns-core-modules doesn't handle initial value correctly #6385

Description

Environment
From tns info:
✔ Component nativescript has 4.2.4 version and is up to date.
✔ Component tns-core-modules has 4.2.1 version and is up to date.
✔ Component tns-ios has 4.2.0 version and is up to date.

The bug
The reduce function form ObservableArray doesn't handle initial value correctly. The first value will always be the first item in the list and not the initial value provided as a parameter.

To Reproduce

let products = new ObservableArray([{price:10},{price:20},{price:30}]);

const sum = products.reduce((sum, p, index) => {
  console.log('Iteration', index, 'Sum:', sum, 'Type of sum:', typeof(sum), 'Price:', p.price);
  return sum + p.price;
}, 0);

console.log('Final sum', sum, typeof(sum));

// Output
// Iteration 1 Sum: { "price": 10 } Type of sum: object Price: 20
// Iteration 2 Sum: "[object Object]20" Type of sum: string Price: 30
// Final sum "[object Object]2030" string

Run the code and see that sum will be {price:10} on the first iteration. This is wrong. It should be 0. However, if we didn't pass an initial value as a parameter this would be the expected behaviour.

Expected behavior
On the first iteration the sum should be 0 as it is provided as the initial value.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL