gpt4 book ai didi

javascript - 从对象数组返回单个记录

转载 作者:行者123 更新时间:2023-11-28 05:04:35 25 4
gpt4 key购买 nike

我有一个从数据库查询返回的 obj,它看起来像这样。我希望能够显示“联系人”数组中的单个记录,但我现在确定执行此操作的最佳方法。

以下是我的查询的输出作为示例:

{
"_id": "5886e3692ca4542c453431ee",
"initial": "u",
"last_name": "Peterson",
"first_name": "Pete",
"owner_id": "5886e32c2ca4542c453431ed",
"__v": 1,
"contacts": [
{
"last_name": "Eltoro",
"first_name": "Peter",
"_id": "5886e3f5a219472cac886a9f",
"businesses": [],
"addresses": [],
"phones": [
{
"phone_type": "mobile",
"phone_number": "555-555-999",
"_id": "5886e3f5a219472cac886aa2"
},
{
"phone_type": "home",
"phone_number": "999-876-000",
"_id": "5886e3f5a219472cac886aa1"
}
],
"emails": [
{
"email_address": "tim@time.com",
"_id": "5886e3f5a219472cac886aa0"
}
]
},
{
"college": "University of WA",
"highschool": "Kalaheo",
"birthday": "1990-12-22T08:00:00.000Z",
"last_name": "Smith",
"first_name": "Suzanne",
"_id": "5886fb7fac288c2e31eabe0a",
"businesses": [],
"addresses": [
{
"zip": "98777",
"state": "WA",
"city": "Seattle",
"address_2": "Apt 234",
"address": "124 194th St. SW",
"_id": "5886fb7fac288c2e31eabe0e"
}
],
"phones": [
{
"phone_type": "mobile",
"phone_number": "206-899-9898",
"_id": "5886fb7fac288c2e31eabe0d"
},
{
"phone_type": "home",
"phone_number": "206-789-0987",
"_id": "5886fb7fac288c2e31eabe0c"
}
],
"emails": [
{
"email_type": "personal",
"email_address": "suzanne@smith.com",
"_id": "5886fb7fac288c2e31eabe0b"
}
]
},
{
"last_name": "Alabaster",
"first_name": "Cindy",
"_id": "5888b0bd3c025e54476828d9",
"businesses": [],
"addresses": [],
"phones": [
{
"phone_type": "home",
"phone_number": "999-999-0909",
"_id": "5888b0bd3c025e54476828dc"
},
{
"phone_type": "home",
"phone_number": "000-000-0000",
"_id": "5888b0bd3c025e54476828db"
}
],
"emails": [
{
"email_address": "Some@them.com",
"_id": "5888b0bd3c025e54476828da"
}
]
}
],
"businesses": [],
"addresses": [],
"phones": [
{
"phone_number": "999-999-9999",
"phone_type": "mobile",
"_id": "5886e37f2ca4542c453431ef"
}
],
"emails": []
}

在我的 Angular 代码中,我想抓取其中一个“联系人”记录并显示它。

最好的方法是执行 ng-repeat 并按 id 过滤吗?

最佳答案

我会使用 ng-repeat 和过滤器

$scope.selectedId = "0"

$scope.yourJson = {...}

<input type="text" ng-model="selectedId"></input> // you can enter an id of a contact

<div ng-repeat="contact in yourJson.contacts | filter:{_id: selectedId}">
{{contact.last_name}}
</div> // it will show the last_name of the contact of which you inputed the id

关于javascript - 从对象数组返回单个记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41854835/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com