# Function watchEach

- watchEach<TObsv, TPath, TValue, TTarget>(obj, path, callback, options?): IHandle

- Watches a property for changes on each item in a collection like object. This can be a property on an item in a collection, an item in a collection in the given object or an item in a collection in a nested observable object.

  This function will return a handle that can be used to stop watching the property.

  If the collection contains multiple different types of objects, only objects that have the property being watched will be watched for changes, the other objects will be ignored.

  The callback will be invoked with the new value, old value, and the target object when the property changes. The target object will be the object in the collection that changed. The callback will be invoked for each item in the collection when the property on the object changes. The callback will not be invoked when items are added or removed from the collection unless the options include `initial: true` in which case the callback will be invoked for each item in the collection when the watch is created as well as for any items that are added to the collection after.

  The callback will not be invoked if the value has not changed. This includes if a parent property changes but the resulting value is the same as the previous value.

  If any property in the path after the path to the collection can be undefined, the callback will be invoked with an undefined newValue and an undefined target when a parent property changes to undefined.

      class MyObservable extends observable() {
          property: string = "testProperty";
      }

      class MyClass extends observable() {
          collection: Collection<MyObservable> = new Collection();
      }

      const myClass = new MyClass();
      watchEach(myClass, "collection.[].property", (newValue, oldValue) => {});

      const myCollection = new Collection<MyObservable>();
      watchEach(myCollection, "[].property", (newValue, oldValue) => {});

  #### Type Parameters

  - TObsv extends Observable
  - TPath extends string
  - TValue extends unknown
  - TTarget extends Observable

  #### Parameters

  - obj: TObsv

    The object to watch.

  - path: TPath

    The property path on the given object to watch.

  - callback: ((newValue: TValue, oldValue: TValue, target: TTarget) => void)

    The callback to invoke when the property changes.

    - - (newValue, oldValue, target): void

      - #### Parameters

        - newValue: TValue
        - oldValue: TValue
        - target: TTarget

        #### Returns void

  - `Optional` options: WatchEachOptions

    The options to use for watching the property.

  #### Returns IHandle

  #### Throws

  Error if the object is not observable.

  #### Throws

  Error if the path is to a non-configurable property.

  #### Throws

  Error if the path is not a valid property path.

- watchEach<TObsv, TPath, TValue, TTarget>(obj, path, callback, options?): IHandle

- #### Type Parameters

  - TObsv extends Observable
  - TPath extends `${string}.[].${string}.${string}` | `[].${string}.${string}`
  - TValue
  - TTarget extends unknown

  #### Parameters

  - obj: TObsv
  - path: TPath
  - callback: ((newValue: TValue, oldValue: TValue, target: TTarget) => void)
    - - (newValue, oldValue, target): void

      - #### Parameters

        - newValue: TValue
        - oldValue: TValue
        - target: TTarget

        #### Returns void

  - `Optional` options: WatchEachOptions

  #### Returns IHandle