2

I have a object array list Format like this

var myFormData = [
{
    id: 1,
    name: "first name",
    type: "test",
    root: "/myfolder"
},
{
    id: 3,
    name: "your name",
    type: "test 2",
    root: "/myfolder2"
}, {
    id: 4,
    name: "your test",
    type: "test 3",
    root: "/myfold",
    child: [
        {
            id: 5,
            name: "name",
            type: "testf",
            root: "/myfoldertr"
        },
        {
            id: 6,
            name: "first-name",
            type: "test",
            root: "/myfolderoot",
            child: [
                {
                    id: 8,
                    name: "sub first name",
                    type: "test5",
                    root: "/myfoldertest"
                }, {
                    id: 9,
                    name: "first name root",
                    type: "test9",
                    root: "/myfolder",
                    child: [
                        {
                            id: 10,
                            name: "normal first name",
                            type: "test5",
                            root: "/myfoldertest"
                        }, {
                            id: 11,
                            name: "last first name",
                            type: "test5",
                            root: "/myfoldertest"
                        }
                    ]
                },
                {
                    id: 12,
                    name: "name Name",
                    type: "testf",
                    root: "/myfoldertr"
                }
            ]
        },
        {
            id: 7,
            name: "first name",
            type: "test",
            root: "/myfolder"
        }
    ]
}]

This format is created with database so i cant conform that the datas are exact.Some times they have child or not. I want to delete one object if id is equal to given id (get from programatically) Eg: i want to delete id=11.

1 Answer 1

3

The key point is that you have to look deep into the target array. This code snippet sample used the recursive call to deep into the nested array.

function deleteObj(target, id) {
  if (!Array.isArray(target)) return;
  target.forEach(function(item, index) {
    if (item.child) {
      target = deleteObj(item.child, id);
    }
    if (item.id === 11) {
      target.splice(index, 1);
    }
  });
}

var myFormData = [{
    id: 1,
    name: "first name",
    type: "test",
    root: "/myfolder"
  },
  {
    id: 3,
    name: "your name",
    type: "test 2",
    root: "/myfolder2"
  }, {
    id: 4,
    name: "your test",
    type: "test 3",
    root: "/myfold",
    child: [{
        id: 5,
        name: "name",
        type: "testf",
        root: "/myfoldertr"
      },
      {
        id: 6,
        name: "first-name",
        type: "test",
        root: "/myfolderoot",
        child: [{
            id: 8,
            name: "sub first name",
            type: "test5",
            root: "/myfoldertest"
          }, {
            id: 9,
            name: "first name root",
            type: "test9",
            root: "/myfolder",
            child: [{
              id: 10,
              name: "normal first name",
              type: "test5",
              root: "/myfoldertest"
            }, {
              id: 11,
              name: "last first name",
              type: "test5",
              root: "/myfoldertest"
            }]
          },
          {
            id: 12,
            name: "name Name",
            type: "testf",
            root: "/myfoldertr"
          }
        ]
      },
      {
        id: 7,
        name: "first name",
        type: "test",
        root: "/myfolder"
      }
    ]
  }
];

function deleteObj(target, id) {
  if (!Array.isArray(target)) return;
  target.forEach(function(item, index) {
    if (item.child) {
      target = deleteObj(item.child, id);
    }
    if (item.id === 11) {
      target.splice(index, 1);
    }
  });
}

deleteObj(myFormData, 11);
console.log(myFormData);

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

2 Comments

Thanks Pengyy . :)
@FirozP.J glad it helped. :-)

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.