Reputation: 27
Suppose I have data coming as json in the below format
[
{
"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"
}
]
}
]
I have made a list from clause Name like
c=[joining,xyz]
I want to make a list where the text is coming like
d=[hello Everyone,Some data,hello1,hello2]
Please suggest something about it
Upvotes: 1
Views: 77
Reputation: 22213
Try like this:
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);
}
Upvotes: 1
Reputation: 38114
You can filter and then get desired fields:
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 = [
{
"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"
}
]
}
]
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);
console.log(result);
UPDATE 1:
If you want to filter by ['joining','xyz'];
, then you can use filters
array and check whether data is contained by includes
method:
let filters = ['joining','xyz'];
const result = arr.filter(f => filters.includes(f.clauseName))
.flatMap(r => r.texts.map(t => t.text));
console.log(result);
const arr = [
{
"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"
}
]
}
]
let filters = ['joining','xyz'];
const result = arr.filter(f => filters.includes(f.clauseName))
.sort((a,b) => a.clauseId - b.clauseId)
.flatMap(r => r.texts.map(t => t.text));
console.log(result);
Upvotes: 1