0
var array = [
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{}] }]
  },
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{ key: 'value', subArray: [] }] }]
  },
  { key: 'value', subArray: [] }
];

In my application I've got array of objects. Each objects have field containing another array which can contain another object with field containing array and so on. In some cases objects could be empty. I need write function which will be iterate over all arrays, finds emty objects and remove them. How can I achive this?

4
  • 2
    Can you please share your effort and desired output? Commented Jul 16, 2018 at 11:09
  • 1
    what is an empty object? Commented Jul 16, 2018 at 11:11
  • You can use DFS algorithm here. geeksforgeeks.org/depth-first-search-or-dfs-for-a-graph Commented Jul 16, 2018 at 11:14
  • In fact, I'm trying to build some form builder. In application user cad add some inputs. Each inputs can have subInputs. This varriable that I show above is containing current state of builded form. Empty subArray means that inputs has no subInputs. If subArray contains empty Object that means that inputs had subInputs but subInputs was deleted. Commented Jul 16, 2018 at 11:40

4 Answers 4

1

I think the solution is to use recursion.

var array = [
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{}] }]
  },
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{ key: 'value', subArray: [] }] }]
  },
  { key: 'value', subArray: [] }
];


function lookup(subArr) {
    subArr.forEach((sa, i) => {
        if (Object.keys(sa).length == 0) {
           delete subArr[i];
        } else {
           lookup(sa.subArray);
        }
    });
}


array.forEach(a => {
    lookup(a.subArray);
});


console.log(array);

Sign up to request clarification or add additional context in comments.

Comments

0

try this

for (var i = 0; i < array.length; i++ ) {
  if (array[i].subArray.length != 0) {
    if (Object.keys(array[i].subArray).length)) {
      delete Object.keys(array[i].subArray
    }
  }
}

may be useful for you

1 Comment

This isn't recursive so will only work for a certain depth, also it seems you are deleting arrays that have length which is the opposite of what OP is trying to do.
0

Try like this. Hope it will help to figure out the solution :

for (var i = 0; i < array.length; i++ ){
    if(array[i].subArray)
    {
        removeEmptyItems(array[i].subArray);
    } 
}

removeEmptyItems(item: any) {
    if(item.length == 0){
        delete item;
        return;
    }
    if (item.subArray) {            
        removeEmptyItems(item.subArray);
    } 
}

2 Comments

When You are calling removeEmptyItems You are give it subArray as argument. Thats ok. But then You are checking if its length is equal 0 thats means You are checking if subArray is empty and then if is delete subArr. Thats not what i woudl like to achive. I need check if subArray contains empty object. SubArray can contains infinitive number of objects. I need detect if some of this objects are empty: {} and then a need delete only this empty objects from array, not whole array.
Well, i just wanted to share my idea. You need to modify this :)
0

If you want to find empty objects and remove them (in other words, turn subArray: [{}] to subArray: []), then you can do it with recursive function that replaces all occurrences of arrays with one empty object with an empty arrays:

var array = [
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{}] }]
  },
  {
    key: 'value',
    subArray: [{ key: 'value', subArray: [{ key: 'value', subArray: [] }] }]
  },
  { key: 'value', subArray: [] }
];

function removeEmptyObjects(obj) {

    var objCopy = Object.assign({}, obj);

    Object.keys(objCopy).forEach(key => {

        if (!Array.isArray(objCopy[key])) return;

        var containsEmptyObject = objCopy[key].length === 1 && typeof objCopy[key][0] === 'object' && Object.keys(objCopy[key][0]).length === 0;

        objCopy[key] = containsEmptyObject ? [] : objCopy[key].map(removeEmptyObjects);

    });

    return objCopy;

}

var result = array.map(removeEmptyObjects);

console.log(JSON.stringify(result, null, 2));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.