从AngularJS中的对象数组中通过id获取特定对象 [英] Get specific object by id from array of objects in AngularJS

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

问题描述

我有一个JSON文件,其中包含我想在AngularJS网站上访问的一些数据。现在我想要的是从数组中只获取一个对象。所以我想要例如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 $ http功能如下:

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

$ http.get(data / SampleData.json);

这是有效的。但是我现在如何从我从 $ http.get 获得的数组中获取特定数据对象(通过id)?

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

预先感谢您的帮助。

迎接
Marc

Greets Marc

推荐答案

执行此操作的唯一方法是迭代数组。显然,如果您确定结果是按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中的对象数组中通过id获取特定对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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