【问题标题】:Filter empty arrays from object of arrays in JS从JS中的数组对象中过滤空数组
【发布时间】:2021-08-01 01:37:44
【问题描述】:

我有一个对象数组

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

我想过滤掉这个对象中的所有空数组。因此,在新创建的对象中过滤后,该对象中将没有空数组或任何空键。

【问题讨论】:

  • 什么是空键?你试过什么?
  • "vm": [ {"username": ""}], - 这是您想要过滤掉的那种东西吗?即使数组不为空且对象不为空,也没有任何值 - 将其过滤掉?
  • @JohnTyner 是的,我想过滤掉具有空值的数组,例如 "vm": [ {"username": ""}],
  • @NinaScholz 空键不存在于对象中,空键类似于 name 字段为空,如 {"name": ""}

标签: javascript arrays reactjs object filter


【解决方案1】:

你可以拿

  • 数组过滤
  • 对象过滤

只获取值不等于''的属性。

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】:
    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);
    

    来自https://stackoverflow.com/a/52399512/1772933

    【讨论】:

      猜你喜欢
      • 2022-07-20
      • 2020-02-04
      • 1970-01-01
      • 2020-04-07
      • 1970-01-01
      • 1970-01-01
      • 2019-07-18
      • 1970-01-01
      相关资源
      最近更新 更多