Angular 将键值映射到数据中的其他值
假设我有以下格式的json数据Angular 将键值映射到数据中的其他值,angular,typescript,list,dictionary,collections,Angular,Typescript,List,Dictionary,Collections,假设我有以下格式的json数据 [ { "clauseId": 1, "clauseName": "cover", "texts": [ { "textId": 1, "text": "hello" } ] }, { "clauseId": 3, "clauseNam
[
{
"clauseId": 1,
"clauseName": "cover",
"texts": [
{
"textId": 1,
"text": "hello"
}
]
},
{
"clauseId": 3,
"clauseName": "xyz",
"texts": [
{
"textId": 3,
"text": "hello Everyone"
},
{
"textId": 4,
"text": "Some data"
}
]
}
{
"clauseId": 2,
"clauseName": "joining",
"texts": [
{
"textId": 3,
"text": "hello1"
},
{
"textId": 4,
"text": "hello2"
}
]
}
]
我已经从条款名称中列出了一个列表,如
c=[joining,xyz]
我想列一个清单,上面的文字是什么样子的
d=[hello Everyone,Some data,hello1,hello2]
请提出一些建议尝试以下方式:
result = [];
constructor() {
let texts:any[] = this.data.filter(item => this.c.includes(item.clauseName)).sort((a, b) => a.clauseId - b.clauseId).flatMap(x => x.texts);
this.result = texts.map(x => x.text);
}
您可以筛选并获取所需字段:
let filters = ['hello Everyone','Some data','hello1','hello2'];
const result = arr.filter(f =>
f.texts.some(s => filters.includes(s.text)))
.map(a => a.clauseName);
const arr=[
{
“克劳塞德”:1,
“clauseName”:“封面”,
“案文”:[
{
“textId”:1,
“文本”:“你好”
}
]
},
{
“克劳塞德”:3,
“克劳塞纳”:“xyz”,
“案文”:[
{
“textId”:3,
“文本”:“大家好”
},
{
“textId”:4,
“文本”:“一些数据”
}
]
},
{
“克劳塞德”:2,
“条款名称”:“加入”,
“案文”:[
{
“textId”:3,
“文本”:“你好”
},
{
“textId”:4,
“文本”:“你好”
}
]
}
]
let filters=[‘大家好’、‘一些数据’、‘hello1’、‘hello2’];
const result=arr.filter(f=>f.text.some(s=>filters.includes(s.text))).map(a=>a.clauseName);
控制台日志(结果)代码>反过来,我有一个clauseName列表,我想把它映射到文本,就像在这个例子中,让过滤器=['joining','xyz']@Steppupwell谢谢,但问题是,如果我更改过滤器的模式=['xyz','joining']答案保持不变,但我要求itr将其更改为[hello1,hello2,helloEveryone,Somedata]@adityamishra,所以尝试使用我的回答的第一个版本。如果上述答案不正确,请建议我一个想法,如果我使用过滤器=[xyz,加入]结果为[hello1,hello2,helloEveryone,Somedata],如果我更改筛选器=[joining,xyz],结果为[“hello Everyone”,“Somedata”,“hello1”,“hello2”]@steppup如果筛选器中有多个值,请检查修改后的答案。如果我更改c的索引=[xyz,joining],该怎么办…答案是一样的,,这不是我想要的。请看一看。@Adrita sharma如果c中有多个值怎么办