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

search an item in multidimentionnal array

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

这是我数组的数据:

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' }],
            },
        ],
    },
];

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

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

我的输出:

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

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

你的递归函数离得不远,你需要先检查该项目是否为表格,然后再递归调用它。然后最后只检查循环中的 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 和 find

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))