1

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.

vimuth
  • 5,064
  • 33
  • 79
  • 116
X13Theo
  • 19
  • 4
  • what is your variable 'ups' that you are using in for statement? – Paulo Barbosa Jul 15 '22 at 14:29
  • Your latest error means that the object has no `tt_url` property. Make sure to debug and inspect your variables as you run the code with break points. It is so much easier when you spot that a variable has not the expected value. – trincot Jul 15 '22 at 14:30
  • 2
    change var ups_tt = i['tt_url'] with var ups_tt = ups[i]['tt_url'] . i is just an integer – R4ncid Jul 15 '22 at 14:33
  • hahaha damn im dumb. cheers guys – X13Theo Jul 22 '22 at 13:58

4 Answers4

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];
LostJon
  • 2,287
  • 11
  • 20
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)
});
Tyler2P
  • 2,324
  • 26
  • 22
  • 31
Rajesh
  • 1
  • 2
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;
Mr. Innovator
  • 95
  • 1
  • 6
0

The error is on row var spl = ups_tt.split(/tracknum=(.*)/)[1];, because ups_tt is undefined.

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];

i is the index so it's a number. You could fix it adding either let elem = ups[i] inside the loop, or changing i[property] to ups[i][property]:

for(let i = 0; i < ups.length; i++){
    // A
    let elem = ups[i];
    var ups_tt = elem['tt_url'];
    var unique_id = elem['unique_id'];

    // B
    var ups_tt = ups[i]['tt_url'];
    var unique_id = ups[i]['unique_id'];

    // rest of the loop

Also, since you're also asking how to iterate an array of objects, you could use for .. of, built exactly to do that:

The for...of statement creates a loop iterating over iterable objects, including: built-in String, Array, array-like objects (e.g., arguments or NodeList), TypedArray, Map, Set, and user-defined iterables. It invokes a custom iteration hook with statements to be executed for the value of each distinct property of the object.

Your code would look like this:

for (let elem of ups) {
    var ups_tt = elem['tt_url'];
    var unique_id = elem['unique_id'];

    var spl = ups_tt.split(/tracknum=(.*)/)[1];
    ups_tt = spl.split("&")[0];

    // rest of the loop

There's other ways, for example, this question has a very comprehensive comparison between different ways to loop arrays, or even other types of objects.

Last point, because there's some answers using dot notation: since ups is the array with various elements where every element is an object, instead of elem['tt_url'], you can access their properties using dot, like elem.tt_url, but in this case, that's mostly a matter of preference. In some cases, however, the only option is to use array notation: if you're using a string inside a variable as key, or if the property has some special characters. See this question for more info.

user19513069
  • 317
  • 1
  • 9