繁体   English   中英

在多维数组中搜索一个项目

[英]search an item in multidimentionnal array

我正在尝试使用 JAVASCRIPT function 在多维数组中查找元素,但出现错误

这是我的数组数据:

export const datas = [
    {
        id: 1,
        firstName: 'John',
        tables: [
            { ID: 11, title: 'Lorem' },
            { ID: 12, title: 'Ipsum' },
        ],
    },
    {
        id: 2,
        firstName: 'Doe',
        tables: [
            {
                ID: 22,
                title: 'Arke',
                nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
            },
            { ID: 23, title: 'Korem' },
        ],
    },
    {
        id: 3,
        firstName: 'Brad',
        tables: [
            {
                ID: 30,
                title: 'Mern',
                nodes: [{ name: 'Name4' }, { name: 'Name5' }, { name: 'Name6' }],
            },
            {
                ID: 31,
                title: 'Full',
                nodes: [{ name: 'Name7' }, { name: 'Name8' }, { name: 'Name9' }],
            },
        ],
    },
];

我试过递归 function 但它不起作用,这是我的代码:

export const findById = (arr, id) => {
    for (let o of arr) {
        if (o.tables.length > 0) {
            let a = findById(o.tables.nodes, 'id');
            console.log(a);
        }
    }
};

我想打印 ID 为 22 的 Object,问题是我在每个维度上都没有相同的结构,这仍然让我感到困惑。

我的输入:22

我的 output:

{
    ID: 22,
    title: 'Arke',
    nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }],
},

您知道如何编辑我的 function 以获得我输入的响应吗?

您的递归 function 并不太远,您需要先检查该项目是否作为表格,然后再递归调用它。 最后只检查循环中的 ID。

例如..

 const datas=[{id:1,firstName:"John",tables:[{ID:11,title:"Lorem"},{ID:12,title:"Ipsum"}]},{id:2,firstName:"Doe",tables:[{ID:22,title:"Arke",nodes:[{name:"Name1"},{name:"Name2"},{name:"Name3"}]},{ID:23,title:"Korem"}]},{id:3,firstName:"Brad",tables:[{ID:30,title:"Mern",nodes:[{name:"Name4"},{name:"Name5"},{name:"Name6"}]},{ID:31,title:"Full",nodes:[{name:"Name7"},{name:"Name8"},{name:"Name9"}]}]}]; function findById(arr, ID) { for (const a of arr) { if (a.tables) { const r = findById(a.tables, ID); if (r) return r; } if (a.ID === ID) return a; } } console.log(findById(datas, 22));

如果你只需要嵌套数据,你可以使用 flatMap 并找到

 const findById = (arr, id) => arr.flatMap(d => d.tables).find(t => t.ID === id) const datas = [{ id: 1, firstName: 'John', tables: [{ ID: 11, title: 'Lorem' }, { ID: 12, title: 'Ipsum' }, ], }, { id: 2, firstName: 'Doe', tables: [{ ID: 22, title: 'Arke', nodes: [{ name: 'Name1' }, { name: 'Name2' }, { name: 'Name3' }], }, { ID: 23, title: 'Korem' }, ], }, { id: 3, firstName: 'Brad', tables: [{ ID: 30, title: 'Mern', nodes: [{ name: 'Name4' }, { name: 'Name5' }, { name: 'Name6' }], }, { ID: 31, title: 'Full', nodes: [{ name: 'Name7' }, { name: 'Name8' }, { name: 'Name9' }], }, ], }, ]; console.log(findById(datas, 22))

js 有惊人的数组选项https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array

对您最有帮助的可能是:

这里有些例子

// get the base with id 22
const baseWith22ID = datas.filter(f => f.tables.filter(s => s.id = 22))
// (i guess you want this one) get all elements with id 22
const onlyElementsWith22ID = datas.flatMap(f => f.tables.filter(s => s.id = 22))

暂无
暂无

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

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