1

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?

1
  • What do you mean by and start a function "startSomething" ? Commented Feb 1, 2020 at 9:24

4 Answers 4

1

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

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

Comments

0

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();
}

Comments

0

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 */

Comments

0

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

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.