Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140676

How to filter nested object in javascript

$
0
0

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. 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. 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


Viewing all articles
Browse latest Browse all 140676

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>