【发布时间】:2020-03-29 06:25:09
【问题描述】:
使用 Typescript 在 JSON 中搜索字符串。如何在下面给定的 JSON 数组中搜索字符串? JSON 输入数组如下所示。
let JSON_DATA: Object[] = [
{
id: "1",
name: 'cricket',
subNames: [
{
id: 2,
name: 'bat',
subNames: [
{
id: 3,
name: 'batsman',
subNames: [
{
id: 4,
subNames: 'left',
}
]
}
]
},
{
id: ,
name: 'ball',
subNames: [
{
id: 6,
subNames: 'red',
},
{
id: 7,
name: 'white',
}
]
}
]
},
{
id: 8,
name: 'football',
subNames: [
{
id: 9,
name: 'boot',
subNames: [
{
id: 10,
name: 'white',
}
]
}
]
},
]
我想在上面的 JSON 对象中搜索一个字符串
例如,如果用户键入“white”,那么它应该在整个 JSON 数组中搜索字符串“white”并返回如下 JSON 对象...
let JSON_DATA: Object[] = [
{
id: "1",
name: 'cricket',
subNames: [
{
id: ,
name: 'ball',
subNames: [
{
id: 7,
name: 'white',
}
]
}
]
},
{
id: 8,
name: 'football',
subNames: [
{
id: 9,
name: 'boot',
subNames: [
{
id: 10,
name: 'white',
}
]
}
]
},
]
【问题讨论】:
-
请注意,您使用的是 Javascript 对象。虽然它看起来很相似,但 JSON 是另一回事。
标签: javascript arrays json typescript