萤火科技 hace 11 meses
padre
commit
17a758427e
Se han modificado 1 ficheros con 19 adiciones y 0 borrados
  1. 19 0
      utils/util.js

+ 19 - 0
utils/util.js

@@ -232,3 +232,22 @@ export const rpx2px = (num) => {
 export const formatToLine = value => {
   return value.replace(/([A-Z])/g, '-$1').toLowerCase()
 }
+
+/**
+ * 递归过滤对象中的null
+ * @param {*} object 源对象
+ */
+export const filterNull = object => {
+  if (object === null || typeof object !== 'object') {
+    return object
+  }
+  // if (Array.isArray(object)) {
+  //   return object.filter(item => item !== null).map(filterNull)
+  // }
+  return Object.keys(object).reduce((acc, key) => {
+    if (object[key] !== null && object[key] !== undefined) {
+      acc[key] = filterNull(object[key])
+    }
+    return acc
+  }, {})
+}