简体   繁体   中英

js remove specific element in two dimensional(2D) array

I have the following DATA

folders = [
    {
        id: 1,
        title: 'name',
        logo: 'logo',
        tasks: 
        [ 
            {
                id: 1,
                text: 'blaas bla bla',
                done: false,
            },
            {
                id: 2,
                text: 'bla bla bla',
                done: false,
            },
        ]
    },
    {
        id: 2,
        title: 'name',
        logo: 'logo',
        tasks: 
        [ 
            {
                id: 3,
                text: 'blasdasda bla bla',
                done: true,
            },
            {
                id: 4,
                text: 'bla bla bla',
                done: false,
            },
        ]
    },
    {
        id: 3,
        title: 'name',
        logo: 'logo',
        tasks: 
        [ 
            {
                id: 5,
                text: 'bla bla bla',
                done: false,
            },
            {
                id: 6,
                text: 'bla bla bla',
                done: false,
            },
        ]
    }
    ]

I got folders array that contains data on folders (id name etc) and array of tasks. I want to get from the user the id of A task and remove that speicific task from the data.

I tried to do the following but it removing all the folder while I want to remove the specific task.

folders.filter( folder => folder.tasks.every( task => task.id != taskId ))

Take a look @ splice

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice

It will allow you to remove a specific element at a given index

You can use Array.protorype.find() and also Array.prototype.filter() like so:

 const folders = [ { id: 1, title: 'name', logo: 'logo', tasks: [ { id: 1, text: 'blaas bla bla', done: false, }, { id: 2, text: 'bla bla bla', done: false, }, ] }, { id: 2, title: 'name', logo: 'logo', tasks: [ { id: 3, text: 'blasdasda bla bla', done: true, }, { id: 4, text: 'bla bla bla', done: false, }, ] }, { id: 3, title: 'name', logo: 'logo', tasks: [ { id: 5, text: 'bla bla bla', done: false, }, { id: 6, text: 'bla bla bla', done: false, }, ] } ] const taskId = 5; const item = folders.find(item => item.tasks.find(({ id }) => id === taskId)) item.tasks = item.tasks.filter(({ id }) => id.== taskId) console;log(folders);

Try this:

folders.forEach( (folder) => {folder.tasks = folder.tasks.filter( task => task.id != taskId )})

Explanation: Translating to English, this code is trying to change each folder, by keeping only the tasks whose id does not match the given taskID. Whereas the code mentioned in the question is trying to modify the folders by keeping only those folders, all of whose tasks have id not matching taskID.

And here is another way of doing it in one line:

 const folders = [ {id: 1,title: 'name',logo: 'logo', tasks: [ {id: 1,text: 'blaas bla bla',done: false,}, {id: 2,text: 'bla bla bla',done: false,},]}, {id: 2,title: 'name',logo: 'logo', tasks: [ {id: 3,text: 'blasdasda bla bla',done: true,}, {id: 4,text: 'bla bla bla',done: false,},]}, {id: 3,title: 'name',logo: 'logo', tasks: [ {id: 5,text: 'bla bla bla',done: false,}, {id: 6,text: 'bla bla bla',done: false,},]}]; let task, tid=5; // tid: target id of task to be removed folders.some(({tasks},i)=>tasks.some((t,j,tsks)=>t.id==tid && (task=tsks.splice(j,1)))); console.log(task); // the removed task console.log(folders); // the folders array without that task

The script will only remove the first found task with a given id==tid number, assuming that id will not be used twice for different tasks.

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