简体   繁体   中英

Remove array from object if great grandchildren arrays are empty

So I have an object which has a structure like

object.childArrays.grandchildArrays.greatgrandchildArrays

I want to remove the child array if any of the subsequent arrays are empty. (also can't use es6:'( )

Example object

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

2018 would be removed.

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

Assuming "items" and "docs" are strings or some other sort of item, this would do it:

 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))

However, it is removing not "2018", but just the deep empty array: []. The arrays outside that are not empty, because they each contain something eg "docs".

So make a recursive method to see if all the nested indexes of the arrays have data. Loop over, if it does not remove it.

 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)

However if you have intended your "items" and "docs" to in fact be keys in an object, then this is what you need

Here I am assuming that the array part of the data always starts at the same level, ie you have a year number level, and underneath it an item level, and underneath it a docs level, and only inside that do you begin to have arrays.

 const example = { 2019: { items: { docs: ['a', 'b', 'c'] } }, 2018: { items: { docs: [] } }, 2017: { items: { docs: ['x', 'y', 'z'] } }, }; const out = {} Object.keys(example).forEach(child => { // eg 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM