如何按值搜索javascript对象?

时间:2016-05-26 20:30:26

标签: javascript dynatable

我遇到了一个必须在表格中显示以下响应的场景。

[
  {
    "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
  }]

我在这里有“姓名”作为我的唯一钥匙。

给定一个名称,我想返回具有“名称”的JSON(或javascript对象)。

我真的很困惑。有人可以帮忙吗?

3 个答案:

答案 0 :(得分:3)

如果您只想使用特定name属性处理集合中的对象,是否可以调用Array.prototype.find方法?

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

答案 1 :(得分:0)

你应该Linq Js框架...... 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链接:https://www.nuget.org/packages/linq.js/

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

答案 2 :(得分:0)

我想我想通了。

{{1}}

由于我的名字是唯一的,我使用了findWhere,但如果它不是唯一的,我可以使用find而不是findWhere。 (UnderscoreJS)

感谢您的帮助!