迭代包含对象的数组

编程入门 行业动态 更新时间:2024-10-03 17:17:50
本文介绍了迭代包含对象的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

我想知道如何使用 ng-repeat 查看一个对象中的不同对象?

这是我到目前为止的以下代码,用于遍历数组中对象的 $ID:

{{message.$id}} </div>

这是我的数组的样子:http://imgur/8quH0iW如您所见,数组结构如下所示:
--项目[0]:
-----$Id:xxxxx
-----Message1(唯一ID):
-------------:日期:XX-XX-XXXX"
-------------:消息:随便"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(唯一ID):
-------------:日期:XX-XX-XXXX"
-------------:消息:随便"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
--物品[1]:
-----$Id:xxxxx
-----Message1(唯一ID):
-------------:日期:XX-XX-XXXX"
-------------:消息:随便"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(唯一ID):
-------------:日期:XX-XX-XXXX"
-------------:消息:随便"
-------------:sender: "xxxxxxxxxxxxxxxxxx"

TLDR:如何使用 ng-repeat 重复数组项中的对象?另外,我可以使用 ng-repeat 来获取项目中的最后一个对象吗?例如,最后发送的消息?

谢谢!

解决方案

您没有提到数组中的项是数组还是 JSON 对象.如果您有一组 JSON 对象,那么这将起作用

vm.objectsTest = [{$id : 123}, {$id: 234}];<ul ng-repeat="o in vm.testObjects"><li ng-bind="o.$id"></li></ul>

如果您的项目是数组,那么您将需要访问数组中的对象,如下所示:

vm.objectsTest = [[{$id : 123}], [{$id: 234}]];<ul ng-repeat="o in vm.testObjects"><li ng-bind="o[0].$id"></li></ul>

或者,如果您需要迭代嵌套数组的项目,那么您可能需要嵌套 ng-repeat.然而,最好的选择是更改您的数据结构,因为过度使用 ng-repeat 会导致性能问题.

查看这篇文章以访问 ng-repeat 的最后一个元素ng-repeat 中最后一个元素的不同类

I am wondering how can I view different objects within an object using ng-repeat?

This is the following code that I have so far, to iterate over the $ID of the objects in array:

<div ng-repeat="message in messages"> {{message.$id}} </div>

This is how my array looks like: http://imgur/8quH0iW As you can see, the array structure looks like that:
--Item[0]:
-----$Id:xxxxx
-----Message1(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
--Item[1]:
-----$Id:xxxxx
-----Message1(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"
-----Message2(unique ID):
-------------:date: "XX-XX-XXXX"
-------------:message: "whatever"
-------------:sender: "xxxxxxxxxxxxxxxxxx"

Etc.

TLDR: How can I use ng-repeat to repeat over the objects within an item of an array? Also, can I use ng-repeat to get the last object in an item? For example, the last Message sent?

Thanks!

解决方案

You don't mention whether the items in your array are arrays or JSON objects. If you have an array of JSON objects then this will work

vm.objectsTest = [{$id : 123}, {$id: 234}];
<ul ng-repeat="o in vm.testObjects"><li ng-bind="o.$id"></li></ul>

If your items are arrays then you will need to access the object within the array, like this:

vm.objectsTest = [[{$id : 123}], [{$id: 234}]];
<ul ng-repeat="o in vm.testObjects"><li ng-bind="o[0].$id"></li></ul>

Alternately if you need to iterate over the items of your nested array, then you may need to have nested ng-repeats. However the best option would be to change your data structure as using ng-repeat excessively can create performance issues.

See this post for accessing the last element of ng-repeat Different class for the last element in ng-repeat

这篇关于迭代包含对象的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-21 17:31:07,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1007841.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   对象   迭代

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!