This is an example json object that has been modified to remove certain values.
{
name: { first: 'Robert', middle: '', last: 'Smith' },
age: 25,
DOB: '-',
hobbies: [ 'running', 'coding', '-' ],
education: { highschool: 'N/A', college: 'Yale' }
}
A similar transformation was attempted, but there was difficulty removing the placeholder for an empty item from the array.
{
name: { first: 'Robert', last: 'Smith' },
age: 25,
hobbies: [ 'running', 'coding', <1 empty item> ],
education: { college: 'Yale' }
}
The challenge now is how to effectively eliminate the placeholder for an empty item from the json object.
Here is an implementation of the code:
axios.get("https://coderbyte.com/api/challenges/json/json-cleaning")
.then((res) => {
let parseData = res.data;
const removeValue = (obj) => {
Object.keys(obj).forEach(k =>
// console.log(obj[k])
(obj[k] && obj[k] === "-") &&
delete obj[k] ||
(obj[k] && typeof obj[k] === 'object')
&& removeValue(obj[k]) ||
(!obj[k] && obj[k] !== undefined) &&
delete obj[k] ||
(obj[k] && obj[k] === "N/A") &&
delete obj[k]
);
return obj
}
newData = removeValue(parseData)
console.log(newData)
})