Update nested object property based on the value of another property - JS

a2441918

I have a nested object and I would like to traverse it and update the property of 'error' to true if the 'value' property is an empty/falsy value. If the value of 'value' property is empty string, empty array, empty object, undefined or null, the corresponding 'error' property should be changed to true.

This is what I have:

const data = {
  "item1": {
    "value": 88,
    "error": false
  },
  "item2": {
    "value": 655,
    "error": false
  },
  "item3": false,
  "item4": [],
  "item5": "",
  "item6": "",
  "item7": false,
  "item8": {
    "value": undefined,
    "error": false
  },
  "item9": {
    "value": [],
    "error": false
  },
  "item10": {
    "value": [],
    "error": false
  },
  "item11": {
    "value": [],
    "error": false
  },
  "item12": false,
  "item13": {
    "subItem1": {
      "name": "Country",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    },
    "subItem2": {
      "name": "Group",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    },
    "subItem3": {
      "name": "Product",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    }
  }
}

function iter(o) {
  Object.keys(o).forEach(function(k) {
    if (o[k] !== null && typeof o[k] === 'object') {
      iter(o[k]);
      return;
    }
    if (o['value'] === undefined || !o['value'] || _.isEmpty(o.value)) {
      o['error'] = true
    }
  });
}


iter(data)

console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

expected output:

{
  "item1": {
    "value": 88,
    "error": false
  },
  "item2": {
    "value": 655,
    "error": false
  },
  "item3": false,
  "item4": [],
  "item5": "",
  "item6": "",
  "item7": false,
  "item8": {
    "value": undefined,
    "error": true
  },
  "item9": {
    "value": [],
    "error": true
  },
  "item10": {
    "value": [],
    "error": true
  },
  "item11": {
    "value": [],
    "error": true
  },
  "item12": false,
  "item13": {
    "subItem1": {
      "name": "Country",
      "group": {
        "value": {},
        "error": true
      },
      "instances": []
    },
    "subItem2": {
      "name": "Group",
      "group": {
        "value": {},
        "error": true
      },
      "instances": []
    },
    "subItem3": {
      "name": "Product",
      "group": {
        "value": {},
        "error": true
      },
      "instances": []
    }
  }
}

Please advice.

Jethro91

! check for undefined, 0, '', and null

for [] and {} use JSON.stringify() === '[]' to check if it empty

const data = {
  "item1": {
    "value": 88,
    "error": false
  },
  "item2": {
    "value": 655,
    "error": false
  },
  "item3": false,
  "item4": [],
  "item5": "",
  "item6": "",
  "item7": false,
  "item8": {
    "value": undefined,
    "error": false
  },
  "item9": {
    "value": [],
    "error": false
  },
  "item10": {
    "value": [],
    "error": false
  },
  "item11": {
    "value": [],
    "error": false
  },
  "item12": false,
  "item13": {
    "subItem1": {
      "name": "Country",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    },
    "subItem2": {
      "name": "Group",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    },
    "subItem3": {
      "name": "Product",
      "group": {
        "value": {},
        "error": false
      },
      "instances": []
    }
  }
}

function iter(o) {
  Object.keys(o).forEach(function(k) {
    if (o[k] !== null && typeof o[k] === 'object') {
      iter(o[k]);
      return;
    }
    if (!o['value'] || JSON.stringify(o['value']) === '{}' || JSON.stringify(o['value']) === '[]') {
      if (!o.hasOwnProperty('value')){
        return;
      }
      o['error'] = true
      return;
    }
   
  });
}


iter(data)

console.log(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.20/lodash.min.js"></script>

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Converting JSON to an object based off of another property

分類Dev

Getting a property from current object to use the property value in another

分類Dev

JS Turn an array into Object with property key/value

分類Dev

javascript return property value from nested array of objects based on condition

分類Dev

Accesing object value in array of objects by conditioning another object property

分類Dev

Updating async forEach to update every document property based off property from another collection

分類Dev

Sorting an array based on property value of another array of objects

分類Dev

Gatsby.js: Filter GraphQL query by nested object property

分類Dev

Can't make javascript object property value update

分類Dev

Get index of object in Generic/List based on value of a property

分類Dev

Inline set property of JS Object only if value is defined

分類Dev

How to update key/value of a nested state object (React JS)

分類Dev

Dynamically set object property value

分類Dev

How to update JSONB array based on property name?

分類Dev

lodash: insert property to another property within the same object

分類Dev

How to access an object literal property from another property?

分類Dev

Convert an array of objects to a hash based on a property of the object

分類Dev

Update table property from a value in a same table

分類Dev

Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

分類Dev

Conditional JSON schema based on dynamic property value

分類Dev

How to turn an input text-value into an object's property value in Js?

分類Dev

How to update existing nested object properties based on a list of key-value items

分類Dev

Vue.js 3.0 - Update component UI when store property value changes via composition API

分類Dev

Mongo query existence and value of embedded object property

分類Dev

Assign object property to value if x = null

分類Dev

Get an Object's Property from HashMap Value

分類Dev

Reuse a property value as a variable within the same object

分類Dev

PHP OOP: Can the value of an object property be a function?

分類Dev

Interface of object parameter with default value and default property

Related 関連記事

  1. 1

    Converting JSON to an object based off of another property

  2. 2

    Getting a property from current object to use the property value in another

  3. 3

    JS Turn an array into Object with property key/value

  4. 4

    javascript return property value from nested array of objects based on condition

  5. 5

    Accesing object value in array of objects by conditioning another object property

  6. 6

    Updating async forEach to update every document property based off property from another collection

  7. 7

    Sorting an array based on property value of another array of objects

  8. 8

    Gatsby.js: Filter GraphQL query by nested object property

  9. 9

    Can't make javascript object property value update

  10. 10

    Get index of object in Generic/List based on value of a property

  11. 11

    Inline set property of JS Object only if value is defined

  12. 12

    How to update key/value of a nested state object (React JS)

  13. 13

    Dynamically set object property value

  14. 14

    How to update JSONB array based on property name?

  15. 15

    lodash: insert property to another property within the same object

  16. 16

    How to access an object literal property from another property?

  17. 17

    Convert an array of objects to a hash based on a property of the object

  18. 18

    Update table property from a value in a same table

  19. 19

    Any way to assign a variable (not an object property) another variable's value by reference in JavaScript?

  20. 20

    Conditional JSON schema based on dynamic property value

  21. 21

    How to turn an input text-value into an object's property value in Js?

  22. 22

    How to update existing nested object properties based on a list of key-value items

  23. 23

    Vue.js 3.0 - Update component UI when store property value changes via composition API

  24. 24

    Mongo query existence and value of embedded object property

  25. 25

    Assign object property to value if x = null

  26. 26

    Get an Object's Property from HashMap Value

  27. 27

    Reuse a property value as a variable within the same object

  28. 28

    PHP OOP: Can the value of an object property be a function?

  29. 29

    Interface of object parameter with default value and default property

ホットタグ

アーカイブ