I have a JS object like this:
{"field1":1,"field3":14,"category":9,"completion":null,"creation":"23-04-26", "settings":"[{"ip":""}]"}
I would like to create a function which returns such object without field which is string-array "settings":"[{"ip":""}]" and if this array contains only one element and if this object contains only one key-value pair with value of empty string.
Desired result is:
{"field1":1,"field3":14,"category":9,"completion":null,"creation":"23-04-26"}
Arrays not to be filtered:
{"field1":1,"field3":14,"category":9,"completion":null,"creation":"23-04-26", "settings":"[{"ip":"33333not empty"}]"}
or:
{"field1":1,"field3":14,"category":9,"completion":null,"creation":"23-04-26", "settings":"[{"ip":"345", "dns": "address"}]"}
I wrote the following function:
function filterArrayFields(values) {
const result = {};
const fieldNames = Object.keys(values);
fieldNames.forEach((fieldName) => {
if (typeof values[fieldName] !== 'string') {
result[fieldName] = values[fieldName];
return;
}
try {
const parsedValue = JSON.parse(values[fieldName]);
if (!Array.isArray(parsedValue)) {
result[fieldName] = values[fieldName];
return;
}
if (Array.isArray(parsedValue) && parsedValue.length > 1) {
result[fieldName] = values[fieldName];
return;
}
if (Array.isArray(parsedValue) && parsedValue.length === 1) {
const arrayTypeFieldValue = parsedValue[0];
const [arrayTypeFieldValueName] = Object.keys(arrayTypeFieldValue);
if (arrayTypeFieldValue[arrayTypeFieldValueName] !== '') {
result[fieldName] = values[fieldName];
return;
}
}
} catch (error) {
result[fieldName] = values[fieldName];
}
});
return result;
}
This method is working correctly but it looks like that it is overloaded buy if-s or extra conditions. Is it possible to optimize this function anyhow?
Edit: our target field can have any name, not only ‘settings’ – ‘settings’ here is just an example
2
Answers
I think you could tighten this up significantly by shallow-cloning the input object via the spread operator and conditionally merging the settings back in only if it meets your criteria.
The criteria testing could be handled by a separate dedicated function. Something like this:
JS has a builtin filter function. For the settings value using
JSON.stringify()
andJSON.parse()
will be the easiest way to serialize the array