Delete main array when nested array is empty

Related searches

I got the following array:

var arr = [{
    "mainId": 1,
    "parents": [{
        "parent": 1
      },
      {
        "parent": 2
      }
    ]
  },
  {
    "mainId": 2,
    "parents": [{
      "parent": 3
    }]
  }
]

I'm using this function to delete an specific parent in the parents array

var idToDelete = 2
arr.forEach(function (o) {
  o.parents = o.parents.filter(s => s.id !== idToDelete 
})

This is working fine. But when idToDelete = 3 I want to delete the complete main Item and start a function "startSomething"

So that I'm left with the following output

var arr = [{
  "mainId": 1,
  "parents": [{
      "parent": 1
    },
    {
      "parent": 2
    }
  ]
}]

How could this be implemented?

You can map and then filter the top level array, here is an example:

var arr = [{
    "mainId": 1,
    "parents": [{
        "parent": 1
      },
      {
        "parent": 2
      }
    ]
  },
  {
    "mainId": 2,
    "parents": [{
      "parent": 3
    }]
  }
];

var idToDelete = 3;

arr = arr.map((v) => ({
    ...v,
    parents: v.parents.filter(({
      parent
    }) => parent !== idToDelete)
  }))
  .filter((v) => v.parents.length);

console.log(arr);

How to Remove Empty Array from MultiDimensional Array in PHP, It is easy and simple way to remove empty array. array_filter() function filters the elements of array using a callback function. Let's see example� var array = [1, 2, 3, 4];var evens = _.remove(array, function(n) { return n % 2 === 0;});console.log(array);// => [1, 3]console.log(evens);// => [2, 4] Making a Remove Method. As I mentionmed before, there is no native Array.remove method. The Lodash method does solve this problem, but you may not always want to use Lodash.

Filter the parent array by whether it has a .length after mutating the children. Also make sure to use the .parent property (your objects have no .id property):

const startSomething = () => console.log('startSomething');

var arr = 
[
  {
    "mainId": 1,
    "parents": [
      {
        "parent": 1
      },
      {
        "parent": 2
      }
    ]
  },
  {
    "mainId": 2,
    "parents": [
      {
        "parent": 3
      }
    ]
  }
];

var idToDelete = 3;
arr.forEach(function (o) {
  o.parents = o.parents.filter(s => s.parent !== idToDelete)
});
const newArr = arr.filter(({ parents }) => parents.length);
console.log(newArr);
if (newArr.length !== arr.length) {
  startSomething();
}

Find and delete elements in nested arrays by using for loops , If the elem is an element of a nested array, the whole nested array is deleted. / basic-data-structures/iterate-through-all-an-arrays-items-using-for-loops clear the array, then filter out all empty arrays in your return statement. Im my case, mycellarray has 1 x 26 cell array. Each of these cell arra6 has 709 by 1 cells. Several of these cell arrays are empty, some are partially empty. vertcat --> a cell array with 17018 x 1 cells, each with a single entry (many blank). However, 26*709 is 18434, not 17018

You can use .filter() on the array itself (to remove the main object) with .some() to return true or false depending on whether it should be deleted or not:

const arr = [{ "mainId": 1, "parents": [{ "parent": 1 }, { "parent": 2 } ] }, { "mainId": 2, "parents": [{ "parent": 3 }] } ];

const removeObj = (arr, id, cb) => {
  const res = arr.filter(({parents}) => !parents.some(({parent}) => parent === id));
  const item_removed = res.length !== arr.length;
  return (item_removed && cb(res), item_removed);
}
 
const startSomething = new_arr => console.log("Starting with arr", new_arr);
removeObj(arr, 3, startSomething);
.as-console-wrapper { max-height: 100% !important;} /* ignore */

JavaScript, JavaScript | Remove the last item from an array � How to remove Objects from Associative Array in JavaScript ? How to remove object from array� From an associative array or nested table (but not a varray): DELETE(n) deletes the element whose index is n, if that element exists; otherwise, it does nothing. DELETE(m,n) deletes all elements whose indexes are in the range m.. n, if both m and n exist and m <= n; otherwise, it does nothing.

Here's a alternative method, with a function that uses findIndex to find the object in the array.

Then splices the object if it only has 1 parent. Or splices the parent from object if it has more than 1 parent.

Example snippet:

const deleteParent = (arr, id) =>
{
     let idx = arr.findIndex(x=>x.parents.some(p=> p.parent === id));
     if (idx >= 0 && arr[idx].parents.length === 1 ) {
       let obj = arr[idx];
       // remove object from array and do something
       arr.splice(idx, 1); 
       doSomething(obj);
     }
     else if(idx >= 0){
       let obj = arr[idx];
       let parentIdx = obj.parents.findIndex(x=>x.parent==id);
       // remove parent
       obj.parents.splice( parentIdx, 1);
    }
}

function doSomething (obj) { console.log(`Deleted mainId ${obj.mainId}`) }

var arr = [
  {
    "mainId": 1,
    "parents": [
      {
        "parent": 1
      },
      {
        "parent": 2
      }
    ]
  },
  {
    "mainId": 2,
    "parents": [
      {
        "parent": 3
      }
    ]
  }
];

deleteParent(arr, 3);
console.log(JSON.stringify(arr));

deleteParent(arr, 2);
console.log(JSON.stringify(arr));

JavaScript Array splice(): Delete, Insert, and Replace Elements in an , Also, the splice() method does not remove any elements, in this case, therefore, it returns an empty array. For example: Assuming that you have an array named� DELETE. This procedure has three forms. DELETE removes all elements from a collection. DELETE(n) removes the n th element from an associative array or nested table. If n is null, DELETE(n) does nothing. DELETE(m,n) removes all elements in the range m..n from an associative array or nested table. If m is larger than n or if m or n is null, DELETE(m,n) does nothing.

remove empty values from array javascript Code Example, Get code examples like "remove empty values from array javascript" instantly Basic JavaScript: Use Recursion to Create a Range of Numbers � bbcode Expected the depth of nested jsx elements to be <= 2, but found 3� Once you have data in an array, you can sort it, remove duplicates, reverse its order, extract sections of the array, or search through arrays for specific data. You can also convert an array to a string, transform one array of data into another, and roll up an array into a single value.

Java doesn’t limit you to two-dimensional arrays. Arrays can be nested within arrays to as many levels as your program needs. To declare an array with more than two dimensions, you just specify as many sets of empty brackets as you need. For example: int[][][] threeD = new int[3][3][3];

If you wanted to remove a specific element of a nested array in a two-dimensional array, then you would write: arr[i].splice(j, 1); // assumes i is index of the the nested array and j is the index of the element in the nested array you want to remove.

Comments
  • What do you mean by and start a function "startSomething" ?