Reputation: 13
I have 2 arrays. one is costHeadArray which is as
[
{
"label":"Flight",
"value":"179daf5f-2f89-1d12-40a2-e91a6a732774"
}
]
I am iterating response. My object value is as
{
taxInvoiceId: "b1b4d9ec-f20c-4a33-94c1-af7589e7b15a",
enquiryId: "3db0b4a8-5ab1-4c31-861a-73df36205716",
costHeadId: "179daf5f-2f89-1d12-40a2-e91a6a732774",
costSheetId: "01edd4a1-f1ad-a836-3a19-3a28271f1b57",
...
}
Now in same iteration, I need to check if costHeadArray value matches value in object. costHeadArray can have multiple json objects. only if value in costHeadArray matches with object then i need to enable flag. I tried using includes but it dont works . might be I am doing something wrong
My code
this.airInvoiceService.getTaxInvoiceLog(this.enquiryId).subscribe(
response => {
this.taxInvoiceLogs.unshift(...response);
for(const a of response) {
// {taxInvoiceId: "b1b4d9ec-f20c-4a33-94c1-af7589e7b15a", enquiryId: "3db0b4a8-5ab1-4c31-861a-73df36205716", enquiryRefId: "MTPL-2021-000074", costHeadId: "179daf5f-2f89-1d12-40a2-e91a6a732774", costSheetId: "01edd4a1-f1ad-a836-3a19-3a28271f1b57", …}
console.log(a);
//[{"label":"Flight","value":"e9bf7aa2-c730-8175-1cec-afcd239b0dff"}]
console.log(JSON.stringify(this.costHeadArray));
console.log(a.costHeadId);
if (Object.values(a).includes(this.costHeadArray)){
console.log('Gokul');
}
if (this.costHeadArray.includes(a.costHeadId)) {
console.log('Sam');
this.hideForm = true;
}
}
Upvotes: 0
Views: 63
Reputation: 779
I have write simple script for you, please have a look, I hope it will help you
let costHeadArray = [
{
"label":"Flight",
"value":"179daf5f-2f89-1d12-40a2-e91a6a732774"
}
];
let myObject = {
taxInvoiceId: "b1b4d9ec-f20c-4a33-94c1-af7589e7b15a",
enquiryId: "3db0b4a8-5ab1-4c31-861a-73df36205716",
costHeadId: "179daf5f-2f89-1d12-40a2-e91a6a732774",
costSheetId: "01edd4a1-f1ad-a836-3a19-3a28271f1b57",
}
let hasCostHeadValue = costHeadArray.find(o => o.value == myObject.costHeadId) != null;
console.log(hasCostHeadValue);
///////////////////////////////////////////////////////////////////////////////////
Update your method like this.
this.airInvoiceService.getTaxInvoiceLog(this.enquiryId).subscribe(
response => {
this.taxInvoiceLogs.unshift(...response);
for(const a of response) {
// {taxInvoiceId: "b1b4d9ec-f20c-4a33-94c1-af7589e7b15a", enquiryId: "3db0b4a8-5ab1-4c31-861a-73df36205716", enquiryRefId: "MTPL-2021-000074", costHeadId: "179daf5f-2f89-1d12-40a2-e91a6a732774", costSheetId: "01edd4a1-f1ad-a836-3a19-3a28271f1b57", …}
console.log(a);
//[{"label":"Flight","value":"e9bf7aa2-c730-8175-1cec-afcd239b0dff"}]
console.log(JSON.stringify(this.costHeadArray));
console.log(a.costHeadId);
let hasCostHeadValue = this.costHeadArray.find(o => o.value == a.costHeadId) != null;
if(hasConstHeadValue){
} else {
}
// if (Object.values(a).includes(this.costHeadArray)){
// console.log('Gokul');
// }
// if (this.costHeadArray.includes(a.costHeadId)) {
// console.log('Sam');
// this.hideForm = true;
//}
}
Upvotes: 1
Reputation: 1361
Array.includes
is comparing the array items by reference, not by value, thus:
For the array const values = [ {value: 1}, { value: 2} ]
values.includes(values[0])
will return true, because it re-uses the same {value:1}
-object as the array and both objects have same reference ID.values.includes({value: 1})
will return false, because the objects have the same content but different references.You can use Array.findIndex instead:
const search = {value: 1};
const includes = values.findIndex(item => item.value === search.value) >= 0;
Upvotes: 0