Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
269 views
in Technique[技术] by (71.8m points)

javascript - 从AngularJS中的对象数组中通过id获取特定对象(Get specific object by id from array of objects in AngularJS)

I have a JSON file containing some data I d like to access on my AngularJS website.

(我有一个JSON文件,其中包含我想在AngularJS网站上访问的一些数据。)

Now what I want is to get only one object from the array.

(现在我想要的是从数组中只获取一个对象。)

So I d like for example Item with id 1.

(所以我想要例如ID为1的Item。)

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:

(我想用AngularJS $ http功能加载数据,如下所示:)

$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 ?

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

Thanks in advance for your help.

(在此先感谢您的帮助。)

Greets Marc

(迎接马克)

  ask by mooonli translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Using ES6 solution

(使用ES6解决方案)

For those still reading this answer, if you are using ES6 the find method was added in arrays.

(对于仍在阅读此答案的人,如果您使用的是ES6,则会在数组中添加find方法。)

So assuming the same collection, the solution'd be:

(所以假设相同的集合,解决方案是:)

const foo = { "results": [
    {
        "id": 12,
        "name": "Test"
    },
    {
        "id": 2,
        "name": "Beispiel"
    },
    {
        "id": 3,
        "name": "Sample"
    }
] };
foo.results.find(item => item.id === 2)

I'd totally go for this solution now, as is less tied to angular or any other framework.

(我现在完全采用这种解决方案,因为它与角度或任何其他框架无关。)

Pure Javascript.

(纯Javascript。)

Angular solution (old solution)

(角度解决方案(旧解决方案))

I aimed to solve this problem by doing the following:

(我的目标是通过执行以下操作来解决此问题:)

$filter('filter')(foo.results, {id: 1})[0];

A use case example:

(一个用例示例:)

app.controller('FooCtrl', ['$filter', function($filter) {
    var foo = { "results": [
        {
            "id": 12,
            "name": "Test"
        },
        {
            "id": 2,
            "name": "Beispiel"
        },
        {
            "id": 3,
            "name": "Sample"
        }
    ] };

    // We filter the array by id, the result is an array
    // so we select the element 0

    single_object = $filter('filter')(foo.results, function (d) {return d.id === 2;})[0];

    // If you want to see the result, just check the log
    console.log(single_object);
}]);

Plunker: http://plnkr.co/edit/5E7FYqNNqDuqFBlyDqRh?p=preview

(Plunker: http ://plnkr.co/edit/5E7FYqNNqDuqFBlyDqRh?p =preview)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...