Untitled

mail@pastecode.io avatar
unknown
javascript
a year ago
1.5 kB
3
Indexable
Never
// Function to transform the input JSON object
function transformJson(input) {
    // Initialize the output object with empty "AND" and "OR" arrays
    let output = { "otherAllowedFilters": { "AND": [], "OR": [] } };

    // Iterate over each filter in the input object's "otherAllowedFilters" array
    input.otherAllowedFilters.forEach(filter => {
        // Create a copy of the current filter
        let newFilter = { ...filter };

        // If the current filter has its own "otherAllowedFilters" array
        if (filter.otherAllowedFilters) {
            // Call the transformJson function recursively to transform that array
            newFilter.otherAllowedFilters = transformJson({ "otherAllowedFilters": filter.otherAllowedFilters }).otherAllowedFilters;
        }

        // For each operation in the current filter's "operationsAllowed" array
        filter.operationsAllowed.forEach(operation => {
            // Add the transformed filter to the corresponding array in the output object
            output.otherAllowedFilters[operation].push(newFilter);
        });
    });

    // Return the transformed output object
    return output;
}

// Usage
// Define the input JSON object
let jsonObject1 = {
  "otherAllowedFilters": [
    // ...
  ]
};

// Call the transformJson function to transform the input object
let jsonObject2 = transformJson(jsonObject1);

// Print the transformed object
console.log(JSON.stringify(jsonObject2, null, 2));