Hi there, I’m not super proficient with JavaScript, but am looking to use the JS column to convert a JSON object with a quantity property into multiple JSON objects, which I believe should work.
The following code works in JSFiddle, but I’m not returning any results in the Glide column (nor any errors):
function parseCart() {
let initialObject = p1;
let outputObjects = [];
for (let i = 0; i < initialObject.quantity; i++) {
outputObjects.push(JSON.stringify({
id: initialObject.id,
item: initialObject.item
}, null, 2));
}
return outputObjects.join(',');
}
return parseCart();
p1 is referencing a column with JSON that is formatted like so:
You are trying to return an array. In Glide, you can only return strings. You need to add a .join() to the end of the array object. This should return all JSON objects as a comma delimited string.
Apologies. I glanced at your code quickly and missed the join. I thought it was just returning the full array.
If I were to guess again…
I think it might be your input. It looks like the JSON string isn’t being turned into an object so initialObject is just text and doesn’t have a quantity attribute. I’ll play around a bit to see if that’s the case.