从包含嵌套对象和数组的对象中删除空字符串字段?

4

我之前提出了一个问题,但是有人关闭了那个问题。我真的需要这个答案。所以我又提了一个问题。

我有一个如下所示的对象。我必须从嵌套对象和嵌套数组中删除空字符串字段。我该如何删除它们。

const obj = {
  name: 'Red Boy',
  price: '350',
  originalPrice: '', // Empty string field
  stock: 20,
  category: {
    name: '', // Empty String field
    subCategory: { name: ''} // Empty String filed 
  },
  weight: '90kg',
  dimensions: {
    width: '50cm',
    height: '', // Empty string filed
    length: '70cm'
  },
  suitable: [
     { name: 'Yoga' },
     { name: '' }, // Empty String filed
     { name: 'Winter' }
  ],
  additionalInfo: [
     { field: 'closure', value: 'Button' },
     { field: 'collar', value: ''} // Empty String Field 
  ]
}

在这个混合对象类型中,您可以看到一些子对象和一些子数组。您还可以看到一些没有包含任何值的字段。(我已注释掉该字段)。

实际上,我需要删除该字段。如何从上述混合对象类型中删除该空字符串字段。

谢谢。

我的期望结果-

{
  name: 'Red Boy',
  price: '350',
  // Removed
  stock: 20,
  category: {
    name: '', // Empty String field
    // Removed
  },
  weight: '90kg',
  dimensions: {
    width: '50cm',
    // Removed
    length: '70cm'
  },
  suitable: [
     { name: 'Yoga' },
     //Removed
     { name: 'Winter' }
  ],
  additionalInfo: [
     { field: 'closure', value: 'Button' },
     { field: 'collar', //Removed }
     // Here If this two filed is empty then should remove the whole object
     { field: '', value: '' }
     // Then should remove whole '{ field: '', value: '' }'
  ]
}
4个回答

3
为了实现这一点,我们需要实现一个递归函数,以删除所有嵌套数组和对象中的空字符串。
function rec(obj){
    for(let key of Object.keys(obj)){
        if (obj[key] === ''){
            delete obj[key];
        }
        else if (typeof obj[key] === 'object'){
            obj[key] = rec(obj[key]);
            if (Object.keys(obj[key]).length === 0 ) delete obj[key];
        }
    }
    return Array.isArray(obj) ? obj.filter(val => val) : obj;
}


同样需要注意的是,这不完全是混合的。因为数组是对象的一种特殊类型。

1
以下是一个不可变的方法,用于从对象中删除非空值,同时具有归一化数组内部对象的功能:
const normaliseObject = (obj) =>
  Object.fromEntries(
    Object.entries(obj)
      .filter(([_, value]) => value !== '' && value !== null)
      .map(([key, value]) => (typeof value === 'object' ? [key, normalise(value)] : [key, value])),
  )

// If an element in an array is an object, normalise that too
const normaliseArray = (arr) => arr.map(value => (typeof value === 'object' ? normalise(value) : value))

/**
 * Normalise any object to remove keys whose values are falsey
 * @param obj Object to be normalised
 * @returns Normalised object where falsey values are removed
 */
const normalise = (obj) => {
  return Array.isArray(obj) ? normaliseArray(obj) : normaliseObject(obj)
}

1

const obj = {
  name: 'Red Boy',
  price: '350',
  originalPrice: '', // Empty string field
  stock: 20,
  category: {
    name: '', // Empty String field
    subCategory: { name: ''} // Empty String filed 
  },
  weight: '90kg',
  dimensions: {
    width: '50cm',
    height: '', // Empty string filed
    length: '70cm'
  },
  suitable: [
     { name: 'Yoga' },
     { name: '' }, // Empty String filed
     { name: 'Winter' }
  ],
  additionalInfo: [
     { field: 'closure', value: 'Button' },
     { field: 'collar', value: ''} // Empty String Field 
  ]
}

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

console.log(removeEmptyString(obj))


0

我已经使用了递归来过滤出深层嵌套结构中的空字符串、空对象或空数组。

此函数还会删除这些对象及其没有属性的嵌套对象。

注意:如果提供的初始值不是对象而是数组或字符串,它也将起作用。

var obj={name:"Red Boy",price:"350",originalPrice:"",stock:20,category:{name:"",subCategory:{name:""}},weight:"90kg",dimensions:{width:"50cm",height:"",length:"70cm"},suitable:[{name:"Yoga"},{name:""},{name:"Winter"}],additionalInfo:[{field:"closure",value:"Button"},{field:"collar",value:""}]};

function filt(a) {
  if (typeof a === 'string') return a !== '';
  //if it is a string, then it must not be empty
  else if (Array.isArray(a)) return a.length !== 0
  //if it an arra, then it must have some item
  else if (a instanceof Object) return Object.keys(a).length !== 0;
  //if it is an object, then it must have some property
  return a !== null && a !== undefined
  //else it must not be null or undefined
}

function rec(obj) {
  if (Array.isArray(obj)) {
    //if an value is an array
    return obj.map((a) => rec(a)).filter((a) => filt(a)) //recurse the child first of each value in the array
    //then filter out the value which are either null, empty, undefined or have length 0
  } else if (obj instanceof Object) {
    //if value is an object
    var d = Object.entries(obj).map((a) => ([a[0], rec(a[1])])).filter((a) => filt(a[1]));
    //map through the object.entries and reassign the values to the keys by recurssing over the value to filter out the nested inside irrelevant value
    return Object.fromEntries(d)
    //convert the map into object and return
  } else if (typeof obj === 'string') return obj !== '' ? obj : null
  //f it is a string, it must not be empty else return null
  return obj !== null && obj !== undefined ? obj : null
  //else it must not be null or undefined
}

console.log("For object",rec(obj))
console.log("For Array",rec([{
  name: "Yoga"
}, {
  name: ""
}, {
  name: "Winter"
}]))


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接