Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
371 views
in Technique[技术] by (71.8m points)

javascript - 如何在javascript中过滤嵌套对象(How to filter nested object in javascript [closed])

I have two object as(我有两个对象)

registrationConditions = [ { "registrationTypeClassification": "6f3c383c-506f-453b-8aa8-c9a4acebc5ef", "glue": "AND", "filters": [ { "fieldId": "5b7b316a-76e8-483f-8b44-18a2f30dbbd6", "operator": "Equals", "values": [ "I agree" ] } ], "id": "523d8b34-08a1-4e98-b69a-cbb08ad8f990" }, { "registrationTypeClassification": "6f3c383c-506f-453b-8aa8-c9a4acebc5ef", "glue": "AND", "filters": [ { "fieldId": "d6eda33e-a24a-4e81-a9f9-e101fee47cec", "operator": "Equals", "values": [ "1" ] }, { "fieldId": "47fd3efc-e0ba-4b1b-af46-826f39dbd5ac", "operator": "Equals", "values": [ "xcdsf" ] } ] } ] Second one as :(第二个为:) contactCustomFields = [ "9f3b0f38-130d-48a8-a66a-0fdbf07c2fd3", "47fd3efc-e0ba-4b1b-af46-826f39dbd5ac", "a40ace66-8f9f-4e97-bda1-00d8d3c18194" ] I have to filter out the condition such that if filter.fieldId is not present in the contactCustomField then remove the filter and if in case in the registrationCondition there was only one filter which got removed and then remove that condition object from registrationConditions itself.(我必须过滤掉条件,以便如果contactCustomField没有filter.fieldId ,则删除该过滤器,如果在registrationCondition中只有一个过滤器被删除,然后从registrationConditions自身中删除该condition对象。) How can i achieve that.(我怎样才能做到这一点。) Expected Output :(预期产量:) registrationConditions = [ { "registrationTypeClassification": "6f3c383c-506f-453b-8aa8-c9a4acebc5ef", "glue": "AND", "filters": [ { "fieldId": "47fd3efc-e0ba-4b1b-af46-826f39dbd5ac", "operator": "Equals", "values": [ "xcdsf" ] } ] } ] Since filter.fieldId = 5b7b316a-76e8-483f-8b44-18a2f30dbbd6 is not present in the contactCustonField array , it got removed, eventually since no filter remained in that condition , whole condition got deleted.(由于filter.fieldId = 5b7b316a-76e8-483f-8b44-18a2f30dbbd6不存在于contactCustonField数组中,因此将其删除,最终由于该条件下没有任何过滤器,因此删除了整个条件。) In the same way , in other condition only one filter got deleted.(同样,在其他情况下,仅删除一个过滤器。) let registrationConditions = [ { registrationTypeClassification: "6f3c383c-506f-453b-8aa8-c9a4acebc5ef", glue: "AND", filters: [ { fieldId: "5b7b316a-76e8-483f-8b44-18a2f30dbbd6", operator: "Equals", values: ["I agree"] } ], id: "523d8b34-08a1-4e98-b69a-cbb08ad8f990" }, { registrationTypeClassification: "6f3c383c-506f-453b-8aa8-c9a4acebc5ef", glue: "AND", filters: [ { fieldId: "d6eda33e-a24a-4e81-a9f9-e101fee47cec", operator: "Equals", values: ["1"] }, { fieldId: "47fd3efc-e0ba-4b1b-af46-826f39dbd5ac", operator: "Equals", values: ["xcdsf"] } ] } ]; const contactCustomFields = [ "9f3b0f38-130d-48a8-a66a-0fdbf07c2fd3", "47fd3efc-e0ba-4b1b-af46-826f39dbd5ac", "a40ace66-8f9f-4e97-bda1-00d8d3c18194" ]; const filters = registrationConditions[1].filters; const newfilter = filters.filter(filter => contactCustomFields.includes(filter.fieldId) ); //console.log(newfilter); const filteredCondition = registrationConditions.forEach(condition => { //console.log(condition); var newFilter = []; condition.filters.forEach(filter => { if (contactCustomFields.includes(filter.fieldId)) { console.log("yes"); newfilter.push(filter.fieldId); console.log(filter); console.log(newFilter); } }); condition.filters = newFilter; }); console.log(registrationConditions); checkout the codesandbox here : https://codesandbox.io/s/pensive-cartwright-9i1ep(在此处签出codeandbox: https ://codesandbox.io/s/pensive-cartwright-9i1ep)   ask by Ishant Gaurav translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Working from "I have to filter out the condition such that if filter.fieldId is not present in the contactCustomField then remove the filter and if in case in the registrationCondition there was only one filter which got removed and then remove that condition object from registrationConditions itself. "(从“我必须过滤条件,如果在contactCustomField中不存在filter.fieldId的情况下,则删除该过滤器,并且如果在registrationCondition中只有一个过滤器被删除,然后从registrationConditions自身中删除该条件对象“)

first, let's just look at all the filters from the registrationConditions :(首先,让我们看一下registrationConditions中的所有过滤器:) filters = registrationConditions.map(x => x.filters) right so, now, remove the ones that are not in contactCustomFields :(正确,现在,删除不在contactCustomFields中的那些:) filters = registrationConditions.map(x => x.filters) .filter(x = > contactCustomFields.includes(x.fieldId)) At this point, we know how to process the filters property of each array entry but we also want to modify the array.(至此,我们知道了如何处理每个数组条目的filters属性,但是我们也想修改数组。) To do that we have to work from the array:(为此,我们必须从数组开始工作:) registrationConditions = registrationConditions .map(condition => { filters = condition.filters.filter(f => contactCustomFields.includes(f.fieldId)) if (condition.filters.length === filters.length + 1) { return null } condition.filters = filters return condition }) .filter((obj) => obj /* remove those nulls */) This might lack a bit of polish (didnt actually test it against your objects), but outside of typos and maybe some elementary stuff, I think this is rendering your intent.(这可能没有一点点修饰(实际上没有针对您的对象进行测试),但是除了错别字和一些基本的东西之外,我认为这正在表达您的意图。)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...