Tengo una serie de objetos

{
   "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
}

Quiero filtrar todas las matrices vacías en este objeto. Entonces, después de filtrar en el objeto recién creado, no habrá matrices vacías ni ninguna clave vacía en este objeto.

1
ashfaqrafi 10 may. 2021 a las 19:13

2 respuestas

La mejor respuesta

Podrías tomar

  • filtrado de matrices
  • filtrado de objetos

Y obtenga solo las propiedades con valores diferentes a ''.

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; }
2
Nina Scholz 10 may. 2021 a las 16:45
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);

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

1
John Tyner 10 may. 2021 a las 16:46