Function mutate

Mutates the Value for each of the Keys of a given Object or Array

Example:


const simpleCalc = (v) => v < 0 ? v + 10 : v + 20

const obj = {
a: 1,
b: 2,
c: -1
}

z.mutate(obj, simpleCalc)

// obj is now mutated to:
// {
// a: 21,
// b: 22,
// c: 9,
// }

const arr = [11, 2, -1]
z.mutate(arr, simpleCalc, (v) => v > 10) // with z.isAgree agreement as filter

// arr is now mutated to:
// [31, 2, -1]
  • Parameters

    • oa: object | any[]

      Object or Array

    • mutator: ((value: any, key?: string, oa?: object | any[]) => any)

      the mutation callback, passing the value, key & oa and returns a new value.

        • (value, key?, oa?): any
        • Parameters

          • value: any
          • Optionalkey: string
          • Optionaloa: object | any[]

          Returns any

    • Optionalagreement: any

      an agreement filter as in z.isAgree. If it is true, value is mutated, otherwise it's left intact. Note: isAgree allows "undefined" as a truthy filter value.

    Returns object | any[]

    the mutated object or array