Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
726 B
2
Indexable
Never
javascript
function transformJson(input) {
    let output = { "otherAllowedFilters": { "AND": [], "OR": [] } };

    input.otherAllowedFilters.forEach(filter => {
        let newFilter = { ...filter };
        if (filter.otherAllowedFilters) {
            newFilter.otherAllowedFilters = transformJson({ "otherAllowedFilters": filter.otherAllowedFilters }).otherAllowedFilters;
        }

        filter.operationsAllowed.forEach(operation => {
            output.otherAllowedFilters[operation].push(newFilter);
        });
    });

    return output;
}

// Usage
let jsonObject1 = {
  "otherAllowedFilters": [
    // ...
  ]
};

let jsonObject2 = transformJson(jsonObject1);
console.log(JSON.stringify(jsonObject2, null, 2));