0

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?

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81
  • What do you mean by "display ONLY the JSON which has the unique key?" Do you mean you only want to display a name column? Or do you mean, there are duplicates in the array and you only want to show one record in the table even if you might have the same name twice? Also -- how are you planning to build the table -- programmatically with vanilla JavaScript or are you using a framework? – xianwill May 26 '16 at 20:36
  • @xianwill edited the question. Basically given a name, i need to get the javascript object or JSON which contains the matching value. (for the name). For example, if i give name as "Test" then first JSON should be returned. So that I can display that on a table. – TechnoCorner May 26 '16 at 20:54
  • @TechnoCorner What if there are multiple objects with same name? – Nenad Vracar May 26 '16 at 21:07
  • No. They are unique. I think i figured out `_.find(results.attributes, {name: "Test"})` will return everything that matches. Since they are unique, i could probably use findWhere – TechnoCorner May 26 '16 at 21:08

3 Answers3

3

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'
})
0

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

Fatih GÜRDAL
  • 1,489
  • 1
  • 17
  • 19
0

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!

TechnoCorner
  • 4,879
  • 10
  • 43
  • 81