I am looking to extract information from my javascript array `lstObj' that looks like this:
[
{
"DealerId": "1",
"Type": "Apple,Orange"
},
{
"DealerId": "2",
"Type": "Mango"
},
{
"DealerId": "3",
"Type": "Mango"
}
]
I want to check if there is an item that has Type = Mango and DealerID not = 2. In C# linq I would do something like this:
if(lstObj.Any(x=>x.Type.Split(',').Contains("Mango") && x.DealerID != 2))
{
//Do something
}
How can I achieve the same using javascript?
This should do. If there is no item it will return empty array, otherwise it will return items.
let result = lstObj.filter(dealer => dealer.Type.split(',').includes('Mango') && dealer.DealerId != 2);
let lstObj = [
{
"DealerId": "1",
"Type": "Apple,Orange"
},
{
"DealerId": "2",
"Type": "Mango"
},
{
"DealerId": "3",
"Type": "Mango"
}
]
let result = lstObj.filter(dealer => dealer.Type.split(',').includes('Mango') && dealer.DealerId != 2)
console.log(result)
You can use some
:
var result= array.some(function (item) {
return item.Type.split(",").inlcudes("Mango") && item.DealerID != 2;
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With