我将如何通过以下方式过滤一个对象数组

发布于 2025-02-12 06:13:00 字数 1040 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

山有枢 2025-02-19 06:13:03

一个物品应需要拖曳条件,一个是纳分师长度为两个,另一个是namelist的itemid是连续的。
因此,首先像您一样:

`
let lister3 = userData.filter(names => names?.nameList?.filter(name => name?.name).length > 2)
`

;
然后
`

let lister4 =  lister3.filter(names=>{
let idOfStr = names.nameList?.sort((a,b)=>a.id-b.id).map(item=>item.id).join("");
let resultStr = Array.from(idOfStr.length).fill(+idOfStr[0]).map((item,index)=>+item+index).join('');
return idOfStr === resultStr
})

`

希望这对您有用

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 :

`
let lister3 = userData.filter(names => names?.nameList?.filter(name => name?.name).length > 2)
`

;
then
`

let lister4 =  lister3.filter(names=>{
let idOfStr = names.nameList?.sort((a,b)=>a.id-b.id).map(item=>item.id).join("");
let resultStr = Array.from(idOfStr.length).fill(+idOfStr[0]).map((item,index)=>+item+index).join('');
return idOfStr === resultStr
})

`

hope this is useful for you

西瑶 2025-02-19 06:13:02

过滤器获取一个返回true的函数,如果要保留项目或false(如果不是)。在此功能中,您可以检查namelist的长度,然后在其成员上进行迭代,并确保其id s是连续的:

retult = userData.filter(u => {
    if (u.nameList.length < 2) {
        return false;
    }
    for (let i = 1; i < u.nameList.length; ++i) {
        if (u.nameList[i].id != u.nameList[i - 1].id + 1) {
            return false;
        }
    }
    return true;
});

filter takes a function that returns true if you want to retain the item or false if not. In this function, you could check the length of the nameList, and then iterate over its members and make sure their ids are consecutive:

retult = userData.filter(u => {
    if (u.nameList.length < 2) {
        return false;
    }
    for (let i = 1; i < u.nameList.length; ++i) {
        if (u.nameList[i].id != u.nameList[i - 1].id + 1) {
            return false;
        }
    }
    return true;
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文