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

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

问题描述

我有一个 JSON 文件,其中包含我想在我的 AngularJS 网站上访问的一些数据.现在我想要的是从数组中只获取一个对象.所以我想要例如 id 为 1 的 Item.

数据如下:

{ "结果": [{身份证":1,"name": "测试"},{身份证":2,"name": "贝斯皮尔"},{身份证":3,名称":样品"}] }

我想像这样使用 AngularJS $http 功能加载数据:

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

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

预先感谢您的帮助.

问候马克

解决方案

执行此操作的唯一方法是遍历数组.显然,如果您确定结果按 id 排序,您可以执行 二分搜索

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.

The data looks like this:

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

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

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

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

Thanks in advance for your help.

Greets Marc

解决方案

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天全站免登陆