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

162
Visualizações
Iterate through an array of objects Nodejs

Need some help with this, been stuck for hours.

Trying to iterate through an array of objects in node to grab one of the key's value and perform a regex function.

I keep getting undefined reading errors, the latest one is

Cannot read properties of undefined (reading 'split')

The array is created by calling toArray() on a MongoDB collection find function:

"ups": [
  {
    "_id": "61b5ef3a8bec102408f5289e",
    "article_code": "4325832",
    "order_number": "",
    "status": "shipped",
    "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
    "unique_id": ""
  },
  {
    "_id": "61b5ef3a8bec102408f528b4",
    "article_code": "6242665",
    "order_number": "",
    "status": "shipped",
    "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
    "unique_id": ""
  },
  {
    "_id": "61b5ef3a8bec102408f528ef",
    "article_code": "3610890",
    "order_number": "",
    "status": "shipped",
    "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
    "unique_id": ""
  }
]

Here's my code attempt:

for(let i = 0; i < ups.length; i++) {       
    var ups_tt = i['tt_url'];
    var unique_id = i['unique_id'];
        
    var spl = ups_tt.split(/tracknum=(.*)/)[1];
    ups_tt = spl.split("&")[0];
}

Any help would be appreciated.

about 4 years ago · Santiago Gelvez
3 Respostas
Responde à pergunta

0

so, in your example, i is just a number, so if you try to get a key of a number, then you will get undefined

> let i = 1;
undefined
> i["test"]
undefined

what you should do is reference the array ups with the index:

 for(let i = 0; i < ups.length; i++){
       
        var ups_tt = ups[i].tt_url;
        var unique_id = ups[i].unique_id;
        
        var spl = ups_tt.split(/tracknum=(.*)/)[1];
        ups_tt = spl.split("&")[0];
about 4 years ago · Santiago Gelvez Relatório

0

Try this way:

let y = {
  "ups": [
    {
      "_id": "61b5ef3a8bec102408f5289e",
      "article_code": "4325832",
      "order_number": "",
      "status": "shipped",
      "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
      "unique_id": ""
    }, {
      "_id": "61b5ef3a8bec102408f528b4",
      "article_code": "6242665",
      "order_number": "",
      "status": "shipped",
      "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
      "unique_id": ""
    }, {
      "_id": "61b5ef3a8bec102408f528ef",
      "article_code": "3610890",
      "order_number": "",
      "status": "shipped",
      "tt_url": "https://www.ups.com/track?loc=en_US&tracknum=999&requester=ST/trackdetails",
      "unique_id": ""
    }
  ]
}
    
y.ups.forEach(element => {
  console.log(element)
});
about 4 years ago · Santiago Gelvez Relatório

0

since the url is simple enough, you can just directly split it by "&", no need to use regex

so:

ups_tt = ups_tt.split('&')[2];
var spl = "&" + ups_tt;
about 4 years ago · Santiago Gelvez 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