【问题标题】:Finding nested elements Typescript Array object查找嵌套元素 Typescript Array 对象
【发布时间】:2022-01-13 18:39:04
【问题描述】:

我在一个数组中有一堆内容,类型为:

export interface StaticContent {
  level : number,
  id : string,  
  title: string,
  content : string[] | StaticContent[],
}

我正在尝试搜索要查询的 id 是内容对象的子元素的对象。我想我可能把界面设置错了,但我有心理障碍。

有什么想法吗?

例子:

data = [
{
  level: 0,
  id: 'first-element',
  title: 'Title of the first element'
  content: [
    `I am some random string of content`
  ]
},
{
  level: 1,
  id: 'second-element',
  title: 'Title of the second element'
  content: [
     {
       level: 0,
       id: 'first-sub-element',
       title: 'Title of first sub element'
       content: [
         ` I am some content attached to the first sub element `
       ]
     }
}]

let idToFind = 'first-sub-element'
let element = data.find(t => t.id === idToFind)
let title = element.title

在这个例子中,结果是一个未定义的元素。
我期待

element = {
       level: 0,
       id: 'first-sub-element',
       title: 'Title of first sub element'
       content: [
         ` I am some content attached to the first sub element `
       ]
     }
}

【问题讨论】:

  • 你的代码有什么问题?你期待什么结果?
  • 在上面的例子中,我会得到一个未定义的元素。我希望从第二个元素接收内容数组中的整个第一个元素。
  • 请在您的帖子中添加预期结果
  • data 没有 id=first-sub-element 的元素。 array.find 只查找第一级数组。
  • 我需要一个搜索嵌套元素的方法

标签: javascript arrays typescript


【解决方案1】:

您需要使用递归或在原地进行递归搜索。

这是递归示例:


function searchInContent(idToFind: string, content: StaticContent[] | string[]): StaticContent | null {   
    for (const c of content) {
       if (typeof c === 'string') continue
 
       // now we can assume that c is StaticContent
       if (c.id === idToFind) {
          // FOUND RESULT
          return c
       } 
   
       // Perform deep search in content
       const result = searchInContent(idToFind, c.content)
       if (result !== null) {
          // stop searching and pass found result
          return result
       }
    }
    return null
}

let idToFind = 'first-sub-element'
let element = searchInContent(data)
let title = element.title

【讨论】:

  • 太棒了!一个小错误,searchInContent(data) 缺少idToFind 参数。
【解决方案2】:

您正在尝试查找特定 idcontent 对象。你可以做的是flatMap()内容,然后使用find()找到正确的匹配对象。

const data = [{
    level: 0,
    id: 'first-element',
    title: 'Title of the first element',
    content: [
      `I am some random string of content`
    ]
  },
  {
    level: 1,
    id: 'second-element',
    title: 'Title of the second element',
    content: [{
      level: 0,
      id: 'first-sub-element',
      title: 'Title of first sub element',
      content: [
        ` I am some content attached to the first sub element `
      ]
    }, {
      level: 0,
      id: 'first-sub-elemeeent',
      title: 'Title of first sub element',
      content: [
        ` I am some content attached to the first sub element `
      ]
    }]
  }
];

const idToFind = 'first-sub-element';
const result = data.flatMap(d => d.content).find(c => c.id === idToFind);

console.log(result);

【讨论】:

  • 如果你有多个嵌套元素怎么办。例如,如果我正在搜索“第二子元素”(假设它存在)
  • 这在我现在想从父母身上拿东西的情况下是行不通的,对吧?示例“第一个元素”
  • @ShaunLangley 更新了答案,技巧是首先映射内容,然后在结果数组上运行查找
猜你喜欢
  • 2017-09-10
  • 1970-01-01
  • 1970-01-01
  • 2019-12-16
  • 2019-03-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多