英文:
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 = {
"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(d => !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 = {
"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",
}
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 = {
"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)
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论