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 undefined, otherwise it will return items.
let result = lstObj.filter(dealer => {return 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 => {return 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;
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.