TechnoCorner
TechnoCorner

Reputation: 5155

How to search a javascript object by value?

I'm encountered a scenario where I have to display the following response in a table.

[
  {
    "name": "Test",
    "endPointURI": "http://10.10.10.1:123",
    "successCount": 0,
    "failureCount": 3761,
    "successRate": 0.0,
    "failureRate": 5.980012278871403,
    "totalSent": 3761.0,
    "totalSendRate": 5.980012307671908,
    "latency": 0,
    "oneMinuteSuccessRate": 0.0,
    "fiveMinuteSuccessRate": 0.0,
    "fifteenMinuteSuccessRate": 0.0,
    "oneMinuteFailureRate": 9.971719382874516,
    "fiveMinuteFailureRate": 23.609469948078925,
    "fifteenMinuteFailureRate": 77.78484853747226,
    "oneMinuteSendRate": 9.913005632492993,
    "fiveMinuteSendRate": 23.60190467615165,
    "fifteenMinuteSendRate": 77.7832824814743,
    "lastSentSuccessTime": 0,
    "sendRate": 5.980012307671908
  },
[
  {
    "name": "Test2",
    "endPointURI": "http://10.10.10.1:123",
    "successCount": 0,
    "failureCount": 3761,
    "successRate": 0.0,
    "failureRate": 5.980012278871403,
    "totalSent": 3761.0,
    "totalSendRate": 5.980012307671908,
    "latency": 0,
    "oneMinuteSuccessRate": 0.0,
    "fiveMinuteSuccessRate": 0.0,
    "fifteenMinuteSuccessRate": 0.0,
    "oneMinuteFailureRate": 9.971719382874516,
    "fiveMinuteFailureRate": 23.609469948078925,
    "fifteenMinuteFailureRate": 77.78484853747226,
    "oneMinuteSendRate": 9.913005632492993,
    "fiveMinuteSendRate": 23.60190467615165,
    "fifteenMinuteSendRate": 77.7832824814743,
    "lastSentSuccessTime": 0,
    "sendRate": 5.980012307671908
  }]

I have "Name" as my unique key here.

Given a name, I want to return the JSON (or javascript object) that has the "Name".

I am really confused here. Can someone help?

Upvotes: 0

Views: 209

Answers (3)

TechnoCorner
TechnoCorner

Reputation: 5155

I think i figured it out.

_.findWhere(results.attributes, {name: "Test"}) 

Since my name is unique, I have used findWhere but if it's not unique, i could use find instead of findWhere. (UnderscoreJS)

Thanks for all your help!

Upvotes: 0

Fatih GÜRDAL
Fatih GÜRDAL

Reputation: 1519

You should Linq Js framework... https://linqjs.codeplex.com/

Exmple:

var queryResult = Enumerable.from(jsonArray)
   .where(function (x) { return x.name == "Test2" })
   .orderBy(function (x) { return x.totalSent })
   .select(function (x) { return x.endPointURI }).toArray();

Nuget link:https://www.nuget.org/packages/linq.js/

Nuget Code: Install-Package linq.js -Version 2.2.0.2

Upvotes: 0

Eric Phillips
Eric Phillips

Reputation: 131

If you only want to work with the object in your collection with a particular name property, can you just call the Array.prototype.find method?

let tableData = collection.find(function(myObject) {
    return myObject.name === 'Test'
})

Upvotes: 3

Related Questions