Help

Creating an Array with Unique Values

Topic Labels: Scripting extentions
4360 7
cancel
Showing results for 
Search instead for 
Did you mean: 
Sam_Cederwall
7 - App Architect
7 - App Architect

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

7 Replies 7
Zollie
10 - Mercury
10 - Mercury

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 )
}
Sam_Cederwall
7 - App Architect
7 - App Architect

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.

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

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!

@JonathanBowen, thank you for that write up. It’s been very helpful for me as well!

If you don’t mind me asking, as a follow-up question, instead of returning a list of unique values, is it possible to adapt this script to return the record objects of those unique values in an array?

I’ve been playing around with it and haven’t quite figured out how to do this. Alternatively, I know I can take the array of unique values and use them to filter records in a table and get the desired records that way. Was just hoping to save a step. :slightly_smiling_face:

Thanks!

Hi @StevenB - can you expand on this requirement, I’m not quite getting what you are after?

“return the record objects of those unique values in an array”

What output are you expecting to get?

Thanks!

@JonathanBowen, in your example, you got an array of unique company names as strings from the People table. What I’m trying to do is for each unique company name in the array, I don’t want the unique company name as a string, I want to get the whole record/row that the unique company name came from. The end result would be an array of objects (records/rows).

In short, I’m trying to get unique records from a table.

More specifically, if said table had 10 columns, the criteria for determining if a record is unique is if the combination of values from just two columns is unique.

I hope that makes sense.