Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

215
Visualizações
How would I filter an array of objects by : If id numbers are consecutive

I have a JSON file that has an array of objects with data inside :

[
    {
    "_id": "62bd5fba34a8f1c90303055c",
    "index": 0,
    "email": "mcdonaldholden@xerex.com",
    "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.

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

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;
});
about 4 years ago · Juan Pablo Isaza Relatório

0

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

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda