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

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

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

问题

const obj = {
    "title": "sample",
    "status": 0,
    "creationDate": null,
    "userGroup": "",
    "signatureDelay": "2023-06-05T11:07:18.786Z",
    "listSign": [],
    "priority": false,
    "nature": null,
    "qesData": {
        "signatories": [
            {
                "displayName": "Some user",
                "email": "someuser@hotmail.com",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "test@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "test1@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            }
        ]
    },
    "fileName": "sample.pdf",
};

for (var i = obj.qesData.signatories.length - 1; i >= 0; i--) {
    if (obj.qesData.signatories[i].areWeCreatingNewUser) {
        obj.qesData.signatories.splice(i, 1);
    }
}

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

英文:

I have the following object

const obj = {
    "title": "sample",
    "status": 0,
    "creationDate": null,
    "userGroup": "",
    "signatureDelay": "2023-06-05T11:07:18.786Z",
    "listSign": [],
    "priority": false,
    "nature": null,
    "qesData": {
        "signatories": [
            {
                "displayName": "Some user",
                "email": "someuser@hotmail.com",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "test@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "test1@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            }
        ]
    },
    "fileName": "sample.pdf",
}

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

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

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 的对象。

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

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

答案2

得分: 1

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

const obj = {
    "title": "sample",
    "status": 0,
    "creationDate": null,
    "userGroup": "",
    "signatureDelay": "2023-06-05T11:07:18.786Z",
    "listSign": [],
    "priority": false,
    "nature": null,
    "quesData": {
        "signatories": [
            {
                "displayName": "Some user",
                "email": "someuser@hotmail.com",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "test@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "test1@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            }
        ]
    },
    "fileName": "sample.pdf",
}

obj.qesData.signatories = obj.qesData.signatories.filter(d => !d.areWeCreatingNewUser)
console.log(obj)

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

const obj = {
    "title": "sample",
    "status": 0,
    "creationDate": null,
    "userGroup": "",
    "signatureDelay": "2023-06-05T11:07:18.786Z",
    "listSign": [],
    "priority": false,
    "nature": null,
    "quesData": {
        "signatories": [
            {
                "displayName": "Some user",
                "email": "someuser@hotmail.com",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "test@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "test1@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            }
        ]
    },
    "fileName": "sample.pdf",
}

let data = []
for (let d of obj.qesData.signatories) {
    if (!d.areWeCreatingNewUser) {
        data.push(d)
    }
}

obj.qesData.signatories = data
console.log(obj)
英文:

You can use Array.filter() to do it

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

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

const obj = {
    &quot;title&quot;: &quot;sample&quot;,
    &quot;status&quot;: 0,
    &quot;creationDate&quot;: null,
    &quot;userGroup&quot;: &quot;&quot;,
    &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
    &quot;listSign&quot;: [],
    &quot;priority&quot;: false,
    &quot;nature&quot;: null,
    &quot;qesData&quot;: {
        &quot;signatories&quot;: [
            {
                &quot;displayName&quot;: &quot;Some user&quot;,
                &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
                &quot;firstname&quot;: &quot;User first name&quot;,
                &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
                &quot;lastname&quot;: &quot;User last name&quot;,
                &quot;locale&quot;: &quot;en&quot;,
                &quot;signatoryAttributes&quot;: []
            },
            {
                &quot;email&quot;: &quot;test@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            },
            {
                &quot;email&quot;: &quot;test1@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            }
        ]
    },
    &quot;fileName&quot;: &quot;sample.pdf&quot;,
}

obj.qesData.signatories = obj.qesData.signatories.filter(d =&gt; !d.areWeCreatingNewUser)
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 -->

const obj = {
    &quot;title&quot;: &quot;sample&quot;,
    &quot;status&quot;: 0,
    &quot;creationDate&quot;: null,
    &quot;userGroup&quot;: &quot;&quot;,
    &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
    &quot;listSign&quot;: [],
    &quot;priority&quot;: false,
    &quot;nature&quot;: null,
    &quot;qesData&quot;: {
        &quot;signatories&quot;: [
            {
                &quot;displayName&quot;: &quot;Some user&quot;,
                &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
                &quot;firstname&quot;: &quot;User first name&quot;,
                &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
                &quot;lastname&quot;: &quot;User last name&quot;,
                &quot;locale&quot;: &quot;en&quot;,
                &quot;signatoryAttributes&quot;: []
            },
            {
                &quot;email&quot;: &quot;test@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            },
            {
                &quot;email&quot;: &quot;test1@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            }
        ]
    },
    &quot;fileName&quot;: &quot;sample.pdf&quot;,
}

let data = []
for(d of obj.qesData.signatories){
   if(!d.areWeCreatingNewUser){
     data.push(d)
   }
}

obj.qesData.signatories = data
console.log(obj)

<!-- end snippet -->

答案3

得分: 0

你可以使用filter()方法。

const obj = {
    "title": "sample",
    "status": 0,
    "creationDate": null,
    "userGroup": "",
    "signatureDelay": "2023-06-05T11:07:18.786Z",
    "listSign": [],
    "priority": false,
    "nature": null,
    "qesData": {
        "signatories": [
            {
                "displayName": "Some user",
                "email": "someuser@hotmail.com",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "test@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "test1@hotmail.com",
                "displayName": "",
                "areWeCreatingNewUser": true
            }
        ]
    },
    "fileName": "sample.pdf",
}

obj.qesData.signatories = obj.qesData.signatories.filter(item => !item.areWeCreatingNewUser);
console.log(obj)

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

英文:

You can use filter() method

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

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

const obj = {
    &quot;title&quot;: &quot;sample&quot;,
    &quot;status&quot;: 0,
    &quot;creationDate&quot;: null,
    &quot;userGroup&quot;: &quot;&quot;,
    &quot;signatureDelay&quot;: &quot;2023-06-05T11:07:18.786Z&quot;,
    &quot;listSign&quot;: [],
    &quot;priority&quot;: false,
    &quot;nature&quot;: null,
    &quot;qesData&quot;: {
        &quot;signatories&quot;: [
            {
                &quot;displayName&quot;: &quot;Some user&quot;,
                &quot;email&quot;: &quot;someuser@hotmail.com&quot;,
                &quot;firstname&quot;: &quot;User first name&quot;,
                &quot;id&quot;: &quot;4ffb81f6-6efd-4e41-9168-b032ab3e3b04&quot;,
                &quot;lastname&quot;: &quot;User last name&quot;,
                &quot;locale&quot;: &quot;en&quot;,
                &quot;signatoryAttributes&quot;: []
            },
            {
                &quot;email&quot;: &quot;test@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            },
            {
                &quot;email&quot;: &quot;test1@hotmail.com&quot;,
                &quot;displayName&quot;: &quot;&quot;,
                &quot;areWeCreatingNewUser&quot;: true
            }
        ]
    },
    &quot;fileName&quot;: &quot;sample.pdf&quot;,
}

obj.qesData.signatories = obj.qesData.signatories.filter(item =&gt; !item.areWeCreatingNewUser);
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:

确定