使用Typescript和ionic在嵌套数组中搜索
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Typescript和ionic在嵌套数组中搜索相关的知识,希望对你有一定的参考价值。
我想知道如何使用typescript在嵌套数组中搜索。例如,如果用户输入KNOR我想显示整个组而不是项目。这意味着我想检查名称字段并获得结果。
下面是JSON结构
{
group_id: 61,
sortid: "1",
group_items: [
{
group_id: "61",
id: "1",
code: "1",
name: "ASTRA MARGARINE 1KG",
bulkprice: "800.00",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
picture: [
"img/itemlist/astra/a1.png",
"img/itemlist/astra/a2.png"
],
qty: "2",
active: "0",
availableqty: "100",
itemprice: "400.00"
},
{
group_id: "61",
id: "1",
code: "4",
name: "KNOR CHICKEN CUBE 28S CONTAINER 280G",
bulkprice: "700.00",
description: " Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam at enim sit amet ligula semper tempus.",
picture: [
"img/itemlist/knor/k1.png",
"img/itemlist/knor/k2.png"
],
qty: "10",
active: "0",
availableqty: "1",
itemprice: "25.00"
}
]
},
{
group_id: 64,
sortid: "2",
group_items: [{
group_id: "64",
id: "1",
code: "3",
name: "FLORA MARGARINE LARGE 500G",
bulkprice: "20.00",
description: "Aenean vehicula imperdiet ex, in lacinia magna
bibendum sed.",
picture: [
"img/itemlist/flora/f1.1.png",
"img/itemlist/flora/f1.2.png",
"img/itemlist/flora/f1.png"
],
qty: "1",
active: "0",
availableqty: "500",
itemprice: "700.00"
}]
}
我想要的结果是如果用户搜索Knor
{
group_id: 61,
sortid: "1",
group_items: [
{
id: "1",
code: "1",
name: "ASTRA MARGARINE 1KG",
bulkprice: "800.00",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
picture: [
"img/itemlist/astra/a1.png",
"img/itemlist/astra/a2.png"
],
qty: "2",
active: "0",
availableqty: "100",
itemprice: "400.00"
},
{
id: "1",
code: "4",
name: "KNOR CHICKEN CUBE 28S CONTAINER 280G",
bulkprice: "700.00",
description: " Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam at enim sit amet ligula semper tempus.",
picture: [
"img/itemlist/knor/k1.png",
"img/itemlist/knor/k2.png"
],
qty: "10",
active: "0",
availableqty: "1",
itemprice: "25.00"
}
感谢快速帮助。
答案
你使用以下功能来解决这个问题....
test(){
var data = {
group_id: 61,
sortid: "1",
group_items: [
{
group_id: "61",
id: "1",
code: "1",
name: "ASTRA MARGARINE 1KG",
bulkprice: "800.00",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. ",
picture: [
"img/itemlist/astra/a1.png",
"img/itemlist/astra/a2.png"
],
qty: "2",
active: "0",
availableqty: "100",
itemprice: "400.00"
},
{
group_id: "61",
id: "1",
code: "4",
name: "KNOR CHICKEN CUBE 28S CONTAINER 280G",
bulkprice: "700.00",
description: " Interdum et malesuada fames ac ante ipsum primis in faucibus. Nam at enim sit amet ligula semper tempus.",
picture: [
"img/itemlist/knor/k1.png",
"img/itemlist/knor/k2.png"
],
qty: "10",
active: "0",
availableqty: "1",
itemprice: "25.00"
}
]
}
console.log('name',data)
for(let i=0;i<data.group_items.length;i++){
console.log('name',data.group_items[i].name)
}
}
以上是关于使用Typescript和ionic在嵌套数组中搜索的主要内容,如果未能解决你的问题,请参考以下文章
使用and条件在mongodb文档的嵌套对象中搜索特定字符串