1

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.

4
  • what is an empty key? what have you tried? Commented May 10, 2021 at 16:20
  • "vm": [ {"username": ""}], - is this the kind of thing you want to filter out? Even though the array is not empty and the object is not empty, there's no value - filter that out? Commented May 10, 2021 at 16:20
  • @JohnTyner yes I want to filter out the arrays with null values like "vm": [ {"username": ""}], Commented May 10, 2021 at 16:23
  • @NinaScholz empty key is not present in the object though, empty key would be something like if the name field is empty like {"name": ""} Commented May 10, 2021 at 16:25

2 Answers 2

2

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; }

Sign up to request clarification or add additional context in comments.

Comments

1
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.