javascript - Remove empty values from array of objects only if the value is empty in all objects - Stack Overflow

I'm trying to remove the empty strings in my array.This is my array:let array = [{ name: 'J

I'm trying to remove the empty strings in my array.

This is my array:

let array = [
    { name: 'John', age: '18', address: '' }, 
    { name: 'George', age: '', address: '' }, 
    { name: 'Kevin', age: '25', address: '' }
]

I would like to remove the empty string values ONLY if it's empty in all objects.

desired oute:

[
    { name:'John', age:'18' },
    { name:'George', age:'' },
    { name:'Kevin', age:'25' }
]

This is what I did but it removes EVERY empty string values:

 for (let i = 0; i < array.length; i++) {
    array[i] = Object.fromEntries(Object.entries(array[i]).filter(([_, v]) => v != ''));
 }

I'm trying to remove the empty strings in my array.

This is my array:

let array = [
    { name: 'John', age: '18', address: '' }, 
    { name: 'George', age: '', address: '' }, 
    { name: 'Kevin', age: '25', address: '' }
]

I would like to remove the empty string values ONLY if it's empty in all objects.

desired oute:

[
    { name:'John', age:'18' },
    { name:'George', age:'' },
    { name:'Kevin', age:'25' }
]

This is what I did but it removes EVERY empty string values:

 for (let i = 0; i < array.length; i++) {
    array[i] = Object.fromEntries(Object.entries(array[i]).filter(([_, v]) => v != ''));
 }
Share edited Jan 15, 2024 at 12:55 VLAZ 29.2k9 gold badges63 silver badges84 bronze badges asked Apr 13, 2022 at 8:03 Bar LevinBar Levin 2151 silver badge12 bronze badges 2
  • Can we assume that all objects in the array are the same (i.e. contain the same set of properties)? – yuvin Commented Apr 13, 2022 at 8:10
  • @yuvin Yes , all objects are the same – Bar Levin Commented Apr 13, 2022 at 8:10
Add a ment  | 

3 Answers 3

Reset to default 7

If you don't mind mutating the original array object. Here's a solution utilizing some array functions.

let array = [
  { name: 'John', age: '18', address: '' },
  { name: 'George', age: '', address: '' },
  { name: 'Kevin', age: '25', address: '' }
]

Object.keys(array[0])
  .filter(k => array.every(obj => !obj[k]))
  .forEach(k => array.forEach(obj => delete obj[k]));

console.log(array);

You can solve this issue with a filter method

  • get all the entries to be scanned in the array;
  • for each entry, filter the array with it; if the output array is empty then you can delete this value in the original array

let array = [{name:'John',age:'18',address:''},{name:'George',age:'',address:''},{name:'Kevin',age:'25',address:''}]

const entries = Object.keys(array[0])

entries.forEach(e => {
  if (array.filter(i => i[e]).length === 0) {
    array = array.map(i => {
      delete i[e];
      return i
    })
  }
})

console.log(array)

If you don't want to mutate the original array:

let array = [{name:'John',age:'18',address:''},{name:'George',age:'',address:''},{name:'Kevin',age:'25',address:''}]


let result;

Object.keys(array[0])
.forEach(key => {
    if(array.every(e => e[key] === ''))
        result = array.map(({[key]:_,...rest}) => ({...rest}))
})

console.log(result)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744908633a4600409.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信