Transform data with a function

// If I have enable transform and try something like this, should it work?
// sample data variable
// data = [
// { "id" 1, "notes": "some string with a dog",
// { "id" 2, "notes": "some string with another dog",
// ]

function p(s) {
s.replaceAll("dog", "bunny");
return s;
}
var data = data.map((o) => Object.assign(o, {notes: p(o.notes)}));
return data;

// Now data should look like this
// data = [
// { "id" 1, "notes": "some string with a bunny",
// { "id" 2, "notes": "some string with a bunny",
// ]

hi there, welcome to the community

Yes that logic should be fine, but worth noting you shouldn't need to define a function:

// some sample data that your query may return
let data = [
{ id: 1, notes: "some string with a dog"},
{ id: 2, notes: "some string with another dog"},
]
// run a map function on each element and modify the notes property
return data.map(x => ({ ...x, notes: x.notes.replaceAll('dog','bunny') }));

note: this assumes your query is returning an array, so you may need to formatDataAsArray first
Also note that the sample data you showed isn't in a valid JSON format, in case that was giving errors in your code

Thank you for the help.

1 Like