如何使用 angularjs 获取同一 json 数组中具有 id 的另一个对象

How to get another object in the same json array with id using angularjs

本文关键字:id 一个对象 数组 angularjs 何使用 获取 json      更新时间:2023-09-26

在填写表格时,我像这样发送closedBy所选项目的 ID

if (selectedClosedPlaces != undefined) {
            place.closedBy = selectedClosedPlaces.map(function(g) {
                return g._id;
            });
        } else {
            place.closedBy = ['no_name'];
        }

我正在使用 {{place.closedBy}} 在 HTML 中显示它。
我将拥有这样的 JSON 数据

[{"_id":"56bc720bd0113ab80dd3cb2d","name":{"en":"fshu","de":"hfasj"},"closedBy":["no_name"],"category":{"_id":"56bb2999f197db040dc3b12b","name":{"en":"asdfdsf","de":"asdf"}}}},
{"_id":"56bc7238d0113ab80dd3cb2e","name":{"en":"jsfadk","de":"jkgdj"},"closedBy":["56bc720bd0113ab80dd3cb2d"],"category":{"_id":"56bb2999f197db040dc3b12b","name":{"en":"asdfdsf","de":"asdf"}}}}]

对于第一个,它将为空,所以我给出了一些名称,例如 ['no_name']. 添加第一个后,我只能选择关闭通过使用选择框。
现在我返回第二个的 ID。 我在关闭中使用第二个 ID,请检查上面的 json。 有了那个ID,我需要获取该特定名称
示例:我有56bc720bd0113ab80dd3cb2d id 与此 id 一起我需要显示该 name.en 和 name.de => fshu and hfasj .我正在使用服务来获取值
此处的服务代码

.service('PlaceService', function($http) {
    return {
        init: function(callback) {
            $http.get('/api/places').then(function(place_response) {
                callback(place_response);
            });
        }
    }
})

和控制器

PlaceService.init(function(place_response) {
            $scope.places = place_response.data;
        console.log($scope.places); //giving all the places details like the //above json.
       console.log($scope.places.closedBy); // Not giving closedBy value here

谁能帮我

要在对象数组中找到另一个具有特定 id 的第一项,您可以使用_underscore js lib,如下所示:

var yourWantedId = ...;    
var result = _.findWhere(selectedClosedPlaces, {"_id": yourWantedId });