6

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 of them contains a value, and return the object for example:

var test = "7e9" // Guid

how do i get the object that contains 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 ?

Rafael Herscovici
  • 16,558
  • 19
  • 65
  • 93
  • possible duplicate of [In Javascript, given value, find name from Object literal](http://stackoverflow.com/questions/4432330/in-javascript-given-value-find-name-from-object-literal) – nikc.org Jan 04 '12 at 13:52
  • @nikc.org - its not a duplicate, the other question is using an array in array, while i am using json in array. – Rafael Herscovici Jan 04 '12 at 13:58
  • 1
    Admittedly, not an _exact_ duplicate, but surely you can apply information? http://stackoverflow.com/questions/3515934/json-object-value-based-on-other-object-value http://stackoverflow.com/questions/5181493/how-to-find-a-value-in-a-multidimensional-object-array-in-javascript http://stackoverflow.com/questions/1182082/check-for-a-value-in-a-json-object – nikc.org Jan 04 '12 at 14:04
  • 1
    first example in the last comment is useless, second example uses `filter` which is not crossbrowser, third uses double for loop ( longer to execute ), and look how nice Francesco's answer is :) – Rafael Herscovici Jan 04 '12 at 14:27
  • As I said, probably not an _exact_ duplicate, but all cases were more or less similar to what you were asking. Not using available information and applying it to your specific problem is simply lazy and not constructive. I'm sorry if I seem rude, that's not my intent, but I see too many questions which have been answered many times over already. In the long run that will lead to making information harder to find and SO less valuable for everyone. – nikc.org Jan 04 '12 at 14:38
  • and on the other hand, you prevent inovation. – Rafael Herscovici Jan 04 '12 at 15:13
  • Fair enough. Which innovation(s) do you feel the answers to your trivial question create? – nikc.org Jan 04 '12 at 15:27
  • i have not seen any answer similar to ciccioska. which is the simplest way to achieve what i wanted and it is self explaining. one more thing that i must add, my english is far from perfect, searching for what i needed, with the words i could come up with, have resulted only the first answer sample you gave, while the other three was not even shown. – Rafael Herscovici Jan 04 '12 at 15:33
  • At least one of the questions I linked to had several answers with `for...in` loops, which is a basic JavaScript 101. – nikc.org Jan 04 '12 at 15:36

2 Answers2

8

As associative array you can try this:

for (var s in SoftwareBadges) {
    if (SoftwareBadges[s]["Guid"] == "7e9")
       alert(SoftwareBadges[s]["Title"]);
}
ciccioska
  • 1,291
  • 16
  • 22
3

Array#filter may come handy

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

Note that filter is a part of ECMA5 and may not be available in some browsers.

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

qiao
  • 17,941
  • 6
  • 57
  • 46