ashfaqrafi
ashfaqrafi

Reputation: 500

Filter empty arrays from object of arrays in JS

I have an array of objects

{
   "name":"DLF Shop",
   "merchant_code":"EM499751e",
   "address":"Link Rd, Dhaka 1205, Bangladesh",
   "longitude":90.3937913,
   "latitude":23.7456808,
   "mother_shop_slug":"test-qa-26f7d03d",
   "shop_type":"regular",
   "key_personnel":[
      {
         "name":"",
         "designation":"",
         "phone_no":"",
         "email":""
      }
   ],
   "category_head":[
      {
         "username":""
      }
   ],
   "bdm":[
      {
         "username":""
      }
   ],
   "kam":[
      {
         "username":""
      }
   ],
   "vm":[
      {
         "username":""
      }
   ],
   "organisation_type":"small",
   "is_mother_shop":false,
   "is_delivery_hero_allowed":false,
   "is_cod_allowed":false
}

I want to filter out all the empty arrays in this object. So, after filtering in the newly created object there will be no empty arrays or any empty key in this object.

Upvotes: 1

Views: 932

Answers (2)

Kinglish
Kinglish

Reputation: 23654

var str = '{"name":"DLF Shop","merchant_code":"EM499751e","address":"Link Rd, Dhaka 1205, Bangladesh","longitude":90.3937913,"latitude":23.7456808,"mother_shop_slug":"test-qa-26f7d03d","shop_type":"regular","key_personnel":[{"name":"","designation":"","phone_no":"","email":""}],"category_head":[{"username":""}],"bdm":[{"username":""}],"kam":[{"username":"testforthis"}],"vm":[{"username":""}],"organisation_type":"small","is_mother_shop":false,"is_delivery_hero_allowed":false,"is_cod_allowed":false}';
    let json = JSON.parse(str);


    cleanObject = function(object) {
    Object
        .entries(object)
        .forEach(([k, v]) => {
            if (v && typeof v === 'object')
                cleanObject(v);
            if (v && 
                typeof v === 'object' && 
                !Object.keys(v).length || 
                v === null || 
                v === undefined ||
                v.length === 0
            ) {
                if (Array.isArray(object))
                    object.splice(k, 1);
                else if (!(v instanceof Date))
                    delete object[k];
            }
        });
    return object;
}

let newobj = cleanObject(json);
console.log(newobj);

From https://stackoverflow.com/a/52399512/1772933

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

You could take

  • filtering for arrays
  • filtering for objects

and get only the properties with values unequal to ''.

const
    filter = data => {
        if (Array.isArray(data)) {
            const temp = data.reduce((r, v) => {
                v = filter(v);
                if (v !== '') r.push(v);
                return r;
            }, []);
            return temp.length
                ? temp
                : '';
        }
        if (data && typeof data === 'object') {
            const temp = Object.entries(data).reduce((r, [k, v]) => {
                v = filter(v);
                if (v !== '') r.push([k, v]);
                return r;
            }, []);
            return temp.length
                ? Object.fromEntries(temp)
                : '';
        }
        return data;
    },
    data = { name: "DLF Shop", merchant_code: "EM499751e", address: "Link Rd, Dhaka 1205, Bangladesh", longitude: 90.3937913, latitude: 23.7456808, mother_shop_slug: "test-qa-26f7d03d", shop_type: "regular", key_personnel: [{ name: "", designation: "", phone_no: "", email: "" }], category_head: [{ username: "" }], bdm: [{ username: "" }], kam: [{ username: "" }], vm: [{ username: "" }], organisation_type: "small", is_mother_shop: false, is_delivery_hero_allowed: false, is_cod_allowed: false },
    result = filter(data);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Upvotes: 2

Related Questions