Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How Can I remove "1 empty item" from the json object in javascript

The sample json object. I tried to remove all "-", "N/A", and "" from this JSON object.

{
  name: { first: 'Robert', middle: '', last: 'Smith' },
  age: 25,
  DOB: '-',
  hobbies: [ 'running', 'coding', '-' ],
  education: { highschool: 'N/A', college: 'Yale' }
}

I did something like it. but couldn’t manage to remove <1 empty item> from the array

{
  name: { first: 'Robert', last: 'Smith' },
  age: 25,
  hobbies: [ 'running', 'coding', <1 empty item> ],
  education: { college: 'Yale' }
}

How can I remove the <1 empty item> from the json object.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is my 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)
  })

>Solution :

Using delete is fine on objects, but for arrays, it will remove the property while keeping the array’s .length the same. So, for example, delete-ing the 3rd item of an array of length 3 will result in an array with two array-indexed properties (0 and 1), but keep its length of 3.

Check if the object is an array. If it’s an array, .filter instead.

const payload = {
  name: { first: 'Robert', middle: '', last: 'Smith' },
  age: 25,
  DOB: '-',
  hobbies: [ 'running', 'coding', '-' ],
  education: { highschool: 'N/A', college: 'Yale' }
};
const isGoodValue = val => val && val !== '-' && val !== 'N/A';

const removeBadValues = (obj) => {
  if (Array.isArray(obj)) {
    return obj
      .filter(isGoodValue)
      .map(removeBadValues);
  }
  if (typeof obj === 'object') {
    for (const [key, value] of Object.entries(obj)) {
      if (!isGoodValue(value)) {
        delete obj[key];
      } else {
        obj[key] = removeBadValues(value);
      }
    }
  }
  return obj;
}
const newData = removeBadValues(payload)
console.log(newData)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading