简体   繁体   中英

How to remove element from nested array in JavaScript?

Say I have a nested array like below.Now i want to remove elements but not children.i want to make it's children value to parent array/children value.

const steps =  [
       {
         step_id: 100,
         type: 'TEXT',
         title: null,
         item_id: 1,
         children: [
           {
             step_id: 102,
             type: 'TEXT',
             title: null,
             item_id: 1,
             children: [
               {
                 step_id: 104,
                 type: 'TEXT',
                 title: null,
                 item_id: 1,
                 children: [

                 ]
               }
             ]
           }
         ]
       }
     ];

For Example I want to remove when step_id=102 and its other elements and make it's children value to it's parent children value.I want something like this result.

result=  steps: [
 {
   step_id: 100,
   type: 'TEXT',
   title: null,
   item_id: 1,
   children: [
     {
       step_id: 104,
       type: 'TEXT',
       title: null,
       item_id: 1,
       children: [

       ]
     }
   ]
 }
]

I tried below code but its not returning right result. Please Help me.

Thank you.

 const steps = [ { step_id: 100, type: 'TEXT', title: null, item_id: 1, children: [ { step_id: 102, type: 'TEXT', title: null, item_id: 1, children: [ { step_id: 104, type: 'TEXT', title: null, item_id: 1, children: [ ] } ] } ] } ]; const removeItem ={step_id: 102, type: "TEXT", title: null, item_id: 1, children: Array(0)}; const removeElement = (steps, removeItem) => { steps.forEach((step) => { if (step.step_id == removeItem.step_id) { steps = step.children; } else { removeElement(step.children, removeItem); } }); //console.log(steps); return steps }; console.log(removeElement(steps, removeItem))

You're just reassigning the steps parameter variable, so the original object isn't mutated.
Use splice to replace the found node with its children inserted.

Personally I would have preferred flatMap and a little bit of object cloning. But this is fine if you're okay with the mutation of the original object.

 const steps = [{ step_id: 100, type: 'TEXT', title: null, item_id: 1, children: [{ step_id: 102, type: 'TEXT', title: null, item_id: 1, children: [{ step_id: 104, type: 'TEXT', title: null, item_id: 1, children: [ ] }] }] }]; const removeItem = { step_id: 102, type: "TEXT", title: null, item_id: 1, children: Array(0) }; const removeElement = (steps, removeItem) => { steps.forEach((step, i) => { if (step.step_id == removeItem.step_id) { steps.splice(i,1,...step.children); } else { removeElement(step.children, removeItem); } }); //console.log(steps); return steps }; console.log(removeElement(steps, removeItem))

if the step_id is unique, you can try this

const steps = [{
  step_id: 100,
  type: 'TEXT',
  title: null,
  item_id: 1,
  children: [{
    step_id: 102,
    type: 'TEXT',
    title: null,
    item_id: 1,
    children: [{
      step_id: 104,
      type: 'TEXT',
      title: null,
      item_id: 1,
      children: [

      ]
    }]
  }]
}];

const removeItem = {
  step_id: 102,
  type: "TEXT",
  title: null,
  item_id: 1,
  children: Array(0)
};

const removeElement = (steps, removeItemId) => {
  if (!steps || steps.length === 0) {
    return false;
  }

  var id = null;
  steps.some((item, i) => {
    id = (item.step_id === removeItemId) ? i : null;
    return i !== null;
  });

  if (id !== null) {
    steps.splice(id, 1);
    return true;
  }

  return steps.some(item => removeElement(item.children, removeItemId));
};

console.log(removeElement(steps, removeItem.step_id)); // should be true

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