从AngularJS JSON数组获取由ID特定的JSON对象 [英] Get specific JSON object by id from JSON array in AngularJS

查看:141
本文介绍了从AngularJS JSON数组获取由ID特定的JSON对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含一些数据,我倒要在我的网站上AngularJS访问JSON文件。现在,我要的是从数组中只得到一个对象。所以我D类似于例如项目ID为1。

I have a JSON file containing some data I d like to access on my AngularJS website. Now what I want is to get only one object from the array. So I d like for example Item with id 1.

中的数据是这样的:

{ "results": [
    {
        "id": 1,
        "name": "Test"
    },
    {
        "id": 2,
        "name": "Beispiel"
    },
    {
        "id": 3,
        "name": "Sample"
    }
] }

我倒要加载像这样AngularJS $断点续传功能的数据:

I d like to load the data with AngularJS $http functionality like this:

$ http.get(数据/ SampleData.json);

这是工作。但我怎么能现在得到从阵列我从 $ http.get

which is working. But how can I now get a specific data object (by id) from the array I get from $http.get ?

在此先感谢您的帮助。

问候
马克

推荐答案

要做到这一点的唯一方法是遍历数组。显然,如果你确信该结果是由ID排序,你可以做一个二进制搜索

The only way to do this is to iterate over the array. Obviously if you are sure that the results are ordered by id you can do a binary search

这篇关于从AngularJS JSON数组获取由ID特定的JSON对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆