Creating an Array with Unique Values

Hello,

I have a base designed to track some packages shipped in a “Packages” table. Some of these packages may be linked to the same shipment, as signified by a “Shipment ID” field.

I’m working on creating a script that passes only unique shipment IDs to a “Shipments” table, so that I can link packages to their respective Shipment ID and roll up values from the “Packages” table for some further analysis.

Currently, with my script, I was either hoping to only put Unique IDs into an array, or, from an array I pass ALL the IDs into, create a second array that only captures the unique IDs, and with that, I can create records in the shipment table that I’ll link with either the same script or a different one.

The issue I’m having is that, when I use the output.inspect() or console.log() to view the array, the array displays as [Object, Object, Object, …] and when trying to pull only unique values, even though they might share unique values, it treats them all as distinct.

Any advice on this issue, whether it’s a different way to write this script or a work-around would be very helpful.

Thanks,
Sam

FYI since this is really a JavaScript question, you might have better luck searching for future questions on a coding forum like StackOverflow.

Objects would likely be a better data structure than arrays when you’re trying to ensure unique values. As you’re adding key value pairs to an object, you can check if the key already exists.

if( someObject.hasOwnProperty(someKeyName) === false )  {
    someObject[someKeyName] = someValue
}

The issue I’m having is that, when I use the output.inspect() or console.log() to view the array, the array displays as [Object, Object, Object, …]

Iterate through the array or object, then call the print statements. If it’s an array/object with other nested structures inside of it, you should learn about recursion.

someArray.forEach( (element) => {
    console.log(element)
})

for (var someKeyName in someObject) {
    const someValue = someObject[someKeyName];
    console.log( someKeyName + ": " + someValue )
}
1 Like

Thanks for the reply, I appreciate the help.

I’m a big fan of StackOverflow, it’s been really helpful for a lot of JavaScript questions while I’ve been learning, but, I was having trouble with this one, so I thought I would try here as it’s easier to describe the issues I’m having in relation to Bases.

Thanks again.

2 Likes

Hi @Sam_Cederwall - I did a write up of a similar thing here:

3 Likes

Hi @JonathanBowen,

This is great. I had gotten to an a script that works but I just tried this one out and it’s much more efficient. Thank you.

Loving the new site, it’s a great resource. Keep up the good work!

1 Like