简体   繁体   English

js删除二维(2D)数组中的特定元素

[英]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.我得到了包含文件夹数据(id 名称等)和任务数组的文件夹数组。 I want to get from the user the id of A task and remove that speicific task from the data.我想从用户那里获取 A 任务的 id 并从数据中删除该特定任务。

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看看@ splice

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/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:您可以像这样使用Array.protorype.find()Array.prototype.filter()

 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.解释:翻译成英文,这段代码试图改变每个文件夹,只保留 id 与给定 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.而问题中提到的代码试图通过仅保留那些文件夹来修改文件夹,其所有任务的 id 都与 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.该脚本将仅删除具有给定id==tid编号的第一个找到的任务,假设id不会被两次用于不同的任务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM