Check if the object contains properties with a value

I have the following array:

SoftwareBadges = [ { Title: "Playtech", Guid: "7e9", xPos: "96" }, { Title: "BetSoft", Guid: "890", xPos: "169" }, { Title: "WagerWorks", Guid: "35c", xPos: "242" }, { Title: "Rival", Guid: "c35", xPos: "314" }, { Title: "NetEnt", Guid: "59e", xPos: "387" }, { Title: "MicroGaming", Guid: "19a", xPos: "460" }, { Title: "Cayetano", Guid: "155", xPos: "533" }, { Title: "OpenBet", Guid: "cfe", xPos: "607" }, { Title: "RTG", Guid: "4e6", xPos: "680" }, { Title: "Cryptologic", Guid: "05d", xPos: "753" }, { Title: "CTXM", Guid: "51d", xPos: "827" }, { Title: "Sheriff", Guid: "63e", xPos: "898" }, { Title: "Vegas Tech", Guid: "a50", xPos: "975" }, { Title: "Top Game", Guid: "0d0", xPos: "1048" }, { Title: "Party Gaming", Guid: "46d", xPos: "1121" } ] 

Now, I need to check if on contains one of them and returns an object, for example:

 var test = "7e9" // Guid 

how to get an object containing this guid value? in the sample, it should return the PlayTech object.

in C # using linq, I could do something like: var x = SoftwareBadges.Where(c => c.Guid == test)

how can i do this in javascript?

+6
source share
2 answers

As an associative array, you can try the following:

 for (var s in SoftwareBadges) { if (SoftwareBadges[s]["Guid"] == "7e9") alert(SoftwareBadges[s]["Title"]); } 
+8
source

Array#filter may come in handy

 (SoftwareBadges.filter(function(v) { return v['Guid'] == '7e9'; }))[0] 

Please note that filter is part of ECMA5 and may not be available in some browsers.

You can see the https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/filter detailed document.

+3
source

Source: https://habr.com/ru/post/905097/


All Articles