我将如何通过以下方式过滤一个对象数组
我有一个JSON文件,上面有一个包含数据的对象数组:
[
{
"_id": "62bd5fba34a8f1c90303055c",
"index": 0,
"email": "[email protected]",
"nameList": [
{
"id": 0,
"name": "Wendi Mooney"
},
{
"id": 2,
"name": "Holloway Whitehead"
}
]
},
{
"_id": "62bd5fbac3e5a4fca5e85e81",
"index": 1,
"nameList": [
{
"id": 0,
"name": "Janine Barrett"
},
{
"id": 1,
"name": "Odonnell Savage"
},
{
"id": 2,
"name": "Patty Owen"
}
]
}, ...
我的工作是过滤具有两个以上名称的数组,并且其ID是连续的。 我设法用两个以上的user.name
对用户进行排序,但无法掌握过滤连续的ID号的概念
let lister3 = userData.filter(names => names?.nameList?.filter(name => name?.name).length > 2)
,该概念将我返回具有两个以上用户名的对象。
I have a JSON file that has an array of objects with data inside :
[
{
"_id": "62bd5fba34a8f1c90303055c",
"index": 0,
"email": "[email protected]",
"nameList": [
{
"id": 0,
"name": "Wendi Mooney"
},
{
"id": 2,
"name": "Holloway Whitehead"
}
]
},
{
"_id": "62bd5fbac3e5a4fca5e85e81",
"index": 1,
"nameList": [
{
"id": 0,
"name": "Janine Barrett"
},
{
"id": 1,
"name": "Odonnell Savage"
},
{
"id": 2,
"name": "Patty Owen"
}
]
}, ...
My job is to filter the arrays that have more than two names and if their id are consecutive.
I managed to sort users with more than two user.name
but cant grasp the concept of filtering consecutive id numbers
let lister3 = userData.filter(names => names?.nameList?.filter(name => name?.name).length > 2)
Which returns me the objects with more than two user names.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
一个物品应需要拖曳条件,一个是纳分师长度为两个,另一个是namelist的itemid是连续的。
因此,首先像您一样:
;
然后
`
`
希望这对您有用
a item should need tow conditions,one is nameList length is two ,the other is the itemId of nameList is consecutive;
so first as you do :
;
then
`
`
hope this is useful for you
过滤器
获取一个返回true
的函数,如果要保留项目或false
(如果不是)。在此功能中,您可以检查namelist
的长度,然后在其成员上进行迭代,并确保其id
s是连续的:filter
takes a function that returnstrue
if you want to retain the item orfalse
if not. In this function, you could check the length of thenameList
, and then iterate over its members and make sure theirid
s are consecutive: