skip to Main Content

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": "[email protected]",
                "firstname": "User first name",
                "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                "lastname": "User last name",
                "locale": "en",
                "signatoryAttributes": []
            },
            {
                "email": "[email protected]",
                "displayName": "",
                "areWeCreatingNewUser": true
            },
            {
                "email": "[email protected]",
                "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 [email protected] 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 [email protected]

How can I solve this issue ?

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

3

Answers


  1. 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);
    
    Login or Signup to reply.
  2. You can use Array.filter() to do it

    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": "[email protected]",
                    "firstname": "User first name",
                    "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                    "lastname": "User last name",
                    "locale": "en",
                    "signatoryAttributes": []
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                }
            ]
        },
        "fileName": "sample.pdf",
    }
    
    obj.qesData.signatories = obj.qesData.signatories.filter(d => !d.areWeCreatingNewUser)
    console.log(obj)

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

    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": "[email protected]",
                    "firstname": "User first name",
                    "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                    "lastname": "User last name",
                    "locale": "en",
                    "signatoryAttributes": []
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                }
            ]
        },
        "fileName": "sample.pdf",
    }
    
    let data = []
    for(d of obj.qesData.signatories){
       if(!d.areWeCreatingNewUser){
         data.push(d)
       }
    }
    
    obj.qesData.signatories = data
    console.log(obj)
    Login or Signup to reply.
  3. You can use filter() method

    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": "[email protected]",
                    "firstname": "User first name",
                    "id": "4ffb81f6-6efd-4e41-9168-b032ab3e3b04",
                    "lastname": "User last name",
                    "locale": "en",
                    "signatoryAttributes": []
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                },
                {
                    "email": "[email protected]",
                    "displayName": "",
                    "areWeCreatingNewUser": true
                }
            ]
        },
        "fileName": "sample.pdf",
    }
    
    obj.qesData.signatories = obj.qesData.signatories.filter(item => !item.areWeCreatingNewUser);
    console.log(obj)
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search