Save function with tags/array within table

  • Goal: Goal write a save function to alter only the value that is changed in a table

  • Steps: I have a tried a variety of different JS scripts for the save function but each one I do always re-writes the array and removes all the unchanged arrays and only returns the changeset one

  • Details:

  • example of array in table with text for 'locale' column (key) and 'number' column (tags) with custom entry:

[
{
"locale": "en-au",
"numbers": [
"+ 1-800-000-0000"
]
},
{
"locale": "en-aa",
"numbers": [
"Australia: 1-800-423-2323",
"New Zealand: 00000-3"
]
},
{
"locale": "en-bn",
"numbers": [
"1 888 888 8888"
]
}
]

So a change that could occur is: removal of a value in the number column, adding a value to the array in the number column, or editing a current value in the number column.

what I have tried:
// Get the pending edits from the table
const updatedData = table66.changesetArray || ; // Ensure it's an array

// Ensure existing data is an array
let currentData = Array.isArray(variable_phone_default_table) ? variable_phone_default_table : ;

// Create a lookup object to store existing data
let dataMap = {};

// Populate the lookup with existing data
currentData.forEach(entry => {
if (entry && entry.locale) {
dataMap[entry.locale] = entry.numbers ? [...entry.numbers] : ; // Preserve existing numbers
}
});

// Apply updates from the table's changesetArray
updatedData.forEach(update => {
if (update.locale) {
if (Array.isArray(update.numbers)) {
// Only update the numbers for the edited locale
dataMap[update.locale] = [...update.numbers];
}
}
});

// Convert the updated data back to an array format
let updatedVariable = Object.entries(dataMap).map(([locale, numbers]) => ({
locale,
numbers
}));
// Save back to the variable
variable_phone_default_table.setValue(updatedVariable);

Question: Is it even possible to address a change in a table with tags? if so any recommendations?

Hey there @Ian_Stack,

So you're trying to update your variable with the changes made by user, right?

I provided a potential solution here. Would this be of help?

Hello, there. YES! this worked wonders. much easier than using JS. Thank you so much!

1 Like

Glad I was able to help you out!