【问题标题】:Remove array from object if great grandchildren arrays are empty如果曾孙数组为空,则从对象中删除数组
【发布时间】:2020-10-28 02:39:51
【问题描述】:

所以我有一个具有类似结构的对象

object.childArrays.grandchildArrays.greatgrandchildArrays

如果任何后续数组为空,我想删除子数组。 (也不能用 es6 :'( )

示例对象

object[
  [2019, [items, [docs, ['a', 'b', 'c']], [docs, ['d', 'e', 'f']]]], 
  [2018, [items, [docs, []]]],
  [2017, [items, [docs, ['x', 'y', 'z']]]],
]

2018 将被删除。

object[
  [2019, [items, [docs, ['a', 'b', 'c']], [docs, ['d', 'e', 'f']]]], 
  [2017, [items, [docs, ['x', 'y', 'z']]]],
]

【问题讨论】:

  • 那么什么是示例对象,完成后它应该是什么样子?
  • 任何 sn-p 或您尝试过的东西?
  • 给出一个示例对象,以及您尝试过的内容,人们更有可能渴望提供帮助。否则他们必须花时间试图弄清楚你的意思。例如,通过“后续”,大概您的意思是在层次结构中更深?换句话说,我是否正确理解您要求在树的任何地方都不要留下空数组? --- 太好了,感谢编辑,我会尽力回答。
  • @epascarello 我添加了一个示例来尝试更好地展示我的意思。如果您需要更多信息,请告诉我。
  • @Eureka 让我知道这是否是一个足够好的解释。如果没有,我可以尝试做更多。

标签: javascript jquery arrays javascript-objects


【解决方案1】:

所以做一个递归的方法,看看数组的所有嵌套索引是否都有数据。循环,如果它没有删除它。

const items = [1];
const docs = [2];
const data = [
  [2019, [items, [docs, ['a', 'b', 'c']], [docs, ['d', 'e', 'f']]]], 
  [2018, [items, [docs, []]]],
  [2017, [items, [docs, ['x', 'y', 'z']]]],
];

var hasValues = arr => {
  return arr.every(item => {
    if (Array.isArray(item)) {
        return (item.length === 0) ? false : hasValues(item);
    } else {
      return item !== undefined; // whatever your truthy check should be
    }
  })
}

for (let i = data.length - 1; i>=0; i--) {
  if (!hasValues(data[i])) {
     data.splice(i, 1)
  }
}

console.log(data)

【讨论】:

    【解决方案2】:

    假设 "items" 和 "docs" 是字符串或其他类型的项目,则可以这样做:

    function recursivelyRemoveEmptyArrays(source){
      if (Array.isArray(source)){
        const resultArray =[]
        source.forEach(oneSource=>{
          const oneResult = recursivelyRemoveEmptyArrays(oneSource);
          if (!Array.isArray(oneResult) || oneResult.length>0){
             resultArray.push(oneResult)}
          })
          return resultArray
       } else {
         return source
       }
       
    }
    
    const example=[
      [2019, ["items", ["docs", ['a', 'b', 'c']]],  ["docs", ['d', 'e', 'f']]], 
      [2018, ["items", ["docs", []]]],
      [2017, ["items", ["docs", ['x', 'y', 'z']]]],
    ];
    
    console.log(recursivelyRemoveEmptyArrays(example))

    但是,它删除的不是“2018”,而是深空数组:[]。 外面的数组不是空的,因为它们每个都包含一些东西,例如“文档”。

    【讨论】:

    • 我也需要删除 2018。这是棘手的部分。
    【解决方案3】:

    但是,如果您希望您的“项目”和“文档”实际上是对象中的键,那么这就是您所需要的

    在这里我假设数据的数组部分总是从同一级别开始,即您有一个年份编号级别,在它下面是一个项目级别,在它下面是一个文档级别,只有在这个级别你才开始有数组。

    const example = {
      2019: {
        items: {
          docs: ['a', 'b', 'c']
        }
      },
      2018: {
        items: {
          docs: []
        }
      },
      2017: {
        items: {
          docs: ['x', 'y', 'z']
        }
      },
    };
    
    
    const out = {}
    
    Object.keys(example).forEach(child => { // e.g. 2019
      let nArrayItems = 0;
      Object.keys(example[child]).forEach(grandchild => {
        Object.keys(example[child][grandchild]).forEach(greatgrandchild => {
          nArrayItems += example[child][grandchild][greatgrandchild].length; // This assumes you are confident the greatgrandchild will be an array, so that you can safely rely on its length. If there is a danger that it may be a string, you should be more careful with this step.
    
        })
      })
      if (nArrayItems > 0) {
        out[child] = example[child]
      }
    })
    
    
    console.log(out)

    【讨论】:

    • 嗨@Myles。为什么是-1?我只是在尝试对您的问题中的对象进行不同的解释。如果我的解释有误,请评论为什么解释不正确,否则会劝阻人们试图帮助你。
    • 也许是因为当你最初发布它时,除了标题之外什么都没有。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-06-12
    • 1970-01-01
    相关资源
    最近更新 更多