如何删除所有具有`areWeCreatingNewUser`属性的对象?

huangapple go评论109阅读模式
英文:

How can I remove all objects which are having `areWeCreatingNewUser` property

问题

  1. const obj = {
  2. "title": "sample",
  3. "status": 0,
  4. "creationDate": null,
  5. "userGroup": "",
  6. "signatureDelay": "2023-06-05T11:07:18.786Z",
  7. "listSign": [],
  8. "priority": false,
  9. "nature": null,
  10. "qesData": {
  11. "signatories": [
  12. {
  13. "displayName": "Some user",
  14. "email": "someuser@hotmail.com",
  15. "firstname": "User first name",
  16. "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
  17. "lastname": "User last name",
  18. "locale": "en",
  19. "signatoryAttributes": []
  20. },
  21. {
  22. "email": "test@hotmail.com",
  23. "displayName": "",
  24. "areWeCreatingNewUser": true
  25. },
  26. {
  27. "email": "test1@hotmail.com",
  28. "displayName": "",
  29. "areWeCreatingNewUser": true
  30. }
  31. ]
  32. },
  33. "fileName": "sample.pdf",
  34. };
  35. for (var i = obj.qesData.signatories.length - 1; i >= 0; i--) {
  36. if (obj.qesData.signatories[i].areWeCreatingNewUser) {
  37. obj.qesData.signatories.splice(i, 1);
  38. }
  39. }

这段代码会遍历 qesData => signatories 数组,并删除其中所有具有 areWeCreatingNewUser 属性设置为 true 的对象。这里使用了一个反向遍历的方式,从数组的末尾开始,以避免修改数组时导致索引错位的问题。

英文:

I have the following object

  1. const obj = {
  2. "title": "sample",
  3. "status": 0,
  4. "creationDate": null,
  5. "userGroup": "",
  6. "signatureDelay": "2023-06-05T11:07:18.786Z",
  7. "listSign": [],
  8. "priority": false,
  9. "nature": null,
  10. "qesData": {
  11. "signatories": [
  12. {
  13. "displayName": "Some user",
  14. "email": "someuser@hotmail.com",
  15. "firstname": "User first name",
  16. "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
  17. "lastname": "User last name",
  18. "locale": "en",
  19. "signatoryAttributes": []
  20. },
  21. {
  22. "email": "test@hotmail.com",
  23. "displayName": "",
  24. "areWeCreatingNewUser": true
  25. },
  26. {
  27. "email": "test1@hotmail.com",
  28. "displayName": "",
  29. "areWeCreatingNewUser": true
  30. }
  31. ]
  32. },
  33. "fileName": "sample.pdf",
  34. }

I need to loop through the qesData => signatories array and remove all of the objects there which are having areWeCreatingNewUser property set to true.

So I tried

  1. for(var i = 0;i < obj.qesData.signatories.length;i++) {
  2. if(obj.qesData.signatories[i].areWeCreatingNewUser) {
  3. obj.qesData.signatories.splice(i,1);
  4. }
  5. }

the problem here is that the first time when it founds the user with email test@hotmail.com it remove it with splice at index 1 and after that the obj.qesData.signatories is modified and on next iteration it does not remove the user with email test1@hotmail.com

How can I solve this issue ?

Also I forgot to mention that I need to use old javascript code,filter is not an option

答案1

得分: 1

你可以使用一个过滤器,只保留 areWeCreatingNewUser 不是 true 的对象。

  1. obj.qesData.signatories = obj.qesData.signatories.filter(signatory => signatory.areWeCreatingNewUser !== true);
英文:

You can use a filter to only have objects where areWeCreatingNewUser is not true

  1. obj.qesData.signatories = obj.qesData.signatories.filter(signatory =>
  2. signatory.areWeCreatingNewUser !== true);

答案2

得分: 1

你可以使用Array.filter()来实现它。

  1. const obj = {
  2. "title": "sample",
  3. "status": 0,
  4. "creationDate": null,
  5. "userGroup": "",
  6. "signatureDelay": "2023-06-05T11:07:18.786Z",
  7. "listSign": [],
  8. "priority": false,
  9. "nature": null,
  10. "quesData": {
  11. "signatories": [
  12. {
  13. "displayName": "Some user",
  14. "email": "someuser@hotmail.com",
  15. "firstname": "User first name",
  16. "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
  17. "lastname": "User last name",
  18. "locale": "en",
  19. "signatoryAttributes": []
  20. },
  21. {
  22. "email": "test@hotmail.com",
  23. "displayName": "",
  24. "areWeCreatingNewUser": true
  25. },
  26. {
  27. "email": "test1@hotmail.com",
  28. "displayName": "",
  29. "areWeCreatingNewUser": true
  30. }
  31. ]
  32. },
  33. "fileName": "sample.pdf",
  34. }
  35. obj.qesData.signatories = obj.qesData.signatories.filter(d => !d.areWeCreatingNewUser)
  36. console.log(obj)

更新:对于 ES5 风格,以下是参考代码:

  1. const obj = {
  2. "title": "sample",
  3. "status": 0,
  4. "creationDate": null,
  5. "userGroup": "",
  6. "signatureDelay": "2023-06-05T11:07:18.786Z",
  7. "listSign": [],
  8. "priority": false,
  9. "nature": null,
  10. "quesData": {
  11. "signatories": [
  12. {
  13. "displayName": "Some user",
  14. "email": "someuser@hotmail.com",
  15. "firstname": "User first name",
  16. "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
  17. "lastname": "User last name",
  18. "locale": "en",
  19. "signatoryAttributes": []
  20. },
  21. {
  22. "email": "test@hotmail.com",
  23. "displayName": "",
  24. "areWeCreatingNewUser": true
  25. },
  26. {
  27. "email": "test1@hotmail.com",
  28. "displayName": "",
  29. "areWeCreatingNewUser": true
  30. }
  31. ]
  32. },
  33. "fileName": "sample.pdf",
  34. }
  35. let data = []
  36. for (let d of obj.qesData.signatories) {
  37. if (!d.areWeCreatingNewUser) {
  38. data.push(d)
  39. }
  40. }
  41. obj.qesData.signatories = data
  42. console.log(obj)
英文:

You can use Array.filter() to do it

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const obj = {
  2. &quot;title&quot;: &quot;sample&quot;,
  3. &quot;status&quot;: 0,
  4. &quot;creationDate&quot;: null,
  5. &quot;userGroup&quot;: &quot;&quot;,
  6. &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
  7. &quot;listSign&quot;: [],
  8. &quot;priority&quot;: false,
  9. &quot;nature&quot;: null,
  10. &quot;qesData&quot;: {
  11. &quot;signatories&quot;: [
  12. {
  13. &quot;displayName&quot;: &quot;Some user&quot;,
  14. &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
  15. &quot;firstname&quot;: &quot;User first name&quot;,
  16. &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
  17. &quot;lastname&quot;: &quot;User last name&quot;,
  18. &quot;locale&quot;: &quot;en&quot;,
  19. &quot;signatoryAttributes&quot;: []
  20. },
  21. {
  22. &quot;email&quot;: &quot;test@hotmail.com&quot;,
  23. &quot;displayName&quot;: &quot;&quot;,
  24. &quot;areWeCreatingNewUser&quot;: true
  25. },
  26. {
  27. &quot;email&quot;: &quot;test1@hotmail.com&quot;,
  28. &quot;displayName&quot;: &quot;&quot;,
  29. &quot;areWeCreatingNewUser&quot;: true
  30. }
  31. ]
  32. },
  33. &quot;fileName&quot;: &quot;sample.pdf&quot;,
  34. }
  35. obj.qesData.signatories = obj.qesData.signatories.filter(d =&gt; !d.areWeCreatingNewUser)
  36. console.log(obj)

<!-- end snippet -->


Update: for ES5 style,below is a reference for you

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const obj = {
  2. &quot;title&quot;: &quot;sample&quot;,
  3. &quot;status&quot;: 0,
  4. &quot;creationDate&quot;: null,
  5. &quot;userGroup&quot;: &quot;&quot;,
  6. &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
  7. &quot;listSign&quot;: [],
  8. &quot;priority&quot;: false,
  9. &quot;nature&quot;: null,
  10. &quot;qesData&quot;: {
  11. &quot;signatories&quot;: [
  12. {
  13. &quot;displayName&quot;: &quot;Some user&quot;,
  14. &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
  15. &quot;firstname&quot;: &quot;User first name&quot;,
  16. &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
  17. &quot;lastname&quot;: &quot;User last name&quot;,
  18. &quot;locale&quot;: &quot;en&quot;,
  19. &quot;signatoryAttributes&quot;: []
  20. },
  21. {
  22. &quot;email&quot;: &quot;test@hotmail.com&quot;,
  23. &quot;displayName&quot;: &quot;&quot;,
  24. &quot;areWeCreatingNewUser&quot;: true
  25. },
  26. {
  27. &quot;email&quot;: &quot;test1@hotmail.com&quot;,
  28. &quot;displayName&quot;: &quot;&quot;,
  29. &quot;areWeCreatingNewUser&quot;: true
  30. }
  31. ]
  32. },
  33. &quot;fileName&quot;: &quot;sample.pdf&quot;,
  34. }
  35. let data = []
  36. for(d of obj.qesData.signatories){
  37. if(!d.areWeCreatingNewUser){
  38. data.push(d)
  39. }
  40. }
  41. obj.qesData.signatories = data
  42. console.log(obj)

<!-- end snippet -->

答案3

得分: 0

你可以使用filter()方法。

  1. const obj = {
  2. "title": "sample",
  3. "status": 0,
  4. "creationDate": null,
  5. "userGroup": "",
  6. "signatureDelay": "2023-06-05T11:07:18.786Z",
  7. "listSign": [],
  8. "priority": false,
  9. "nature": null,
  10. "qesData": {
  11. "signatories": [
  12. {
  13. "displayName": "Some user",
  14. "email": "someuser@hotmail.com",
  15. "firstname": "User first name",
  16. "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
  17. "lastname": "User last name",
  18. "locale": "en",
  19. "signatoryAttributes": []
  20. },
  21. {
  22. "email": "test@hotmail.com",
  23. "displayName": "",
  24. "areWeCreatingNewUser": true
  25. },
  26. {
  27. "email": "test1@hotmail.com",
  28. "displayName": "",
  29. "areWeCreatingNewUser": true
  30. }
  31. ]
  32. },
  33. "fileName": "sample.pdf",
  34. }
  35. obj.qesData.signatories = obj.qesData.signatories.filter(item => !item.areWeCreatingNewUser);
  36. console.log(obj)

如果你需要更多帮助,请随时告诉我。

英文:

You can use filter() method

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. const obj = {
  2. &quot;title&quot;: &quot;sample&quot;,
  3. &quot;status&quot;: 0,
  4. &quot;creationDate&quot;: null,
  5. &quot;userGroup&quot;: &quot;&quot;,
  6. &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
  7. &quot;listSign&quot;: [],
  8. &quot;priority&quot;: false,
  9. &quot;nature&quot;: null,
  10. &quot;qesData&quot;: {
  11. &quot;signatories&quot;: [
  12. {
  13. &quot;displayName&quot;: &quot;Some user&quot;,
  14. &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
  15. &quot;firstname&quot;: &quot;User first name&quot;,
  16. &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
  17. &quot;lastname&quot;: &quot;User last name&quot;,
  18. &quot;locale&quot;: &quot;en&quot;,
  19. &quot;signatoryAttributes&quot;: []
  20. },
  21. {
  22. &quot;email&quot;: &quot;test@hotmail.com&quot;,
  23. &quot;displayName&quot;: &quot;&quot;,
  24. &quot;areWeCreatingNewUser&quot;: true
  25. },
  26. {
  27. &quot;email&quot;: &quot;test1@hotmail.com&quot;,
  28. &quot;displayName&quot;: &quot;&quot;,
  29. &quot;areWeCreatingNewUser&quot;: true
  30. }
  31. ]
  32. },
  33. &quot;fileName&quot;: &quot;sample.pdf&quot;,
  34. }
  35. obj.qesData.signatories = obj.qesData.signatories.filter(item =&gt; !item.areWeCreatingNewUser);
  36. console.log(obj)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年6月5日 20:22:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76406373.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定