model and array.filter #379
-
Hi I have a simple Textrude project to demonstrate Name
Produces the output One {Name: "Five"} Five Changing to use the altfilter() as so One [{Name: "Two"}] Five Anyway thank you for a wonderful tool and introducing me to scriban. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 2 replies
-
Glad you're finding it useful :-) I think the mistake you are making is that you are forgetting that the model is an array of objects This is a bit obscured in your example because you only have a single column in your csv but if you consider you could have multiple columns then you can see why your filterModel must be iterating over objects rather than string values. So you've used array.filter to pull out the one object in the array where the Name is "two" then returned that object. The filter operation certainly has created a new array (with one element) but it's a new array that contains the original object from the model. When you set the Name field of that object you're effectively writing back into the model. You can create a new object based on the old one like this (and using piping to better indicate flow)
|
Beta Was this translation helpful? Give feedback.
-
You may also find the debug.dump method useful for tracing changes to objects - see https://github.com/NeilMacMullen/Textrude/blob/main/doc/builtIns.md Also the in-built format conversion libraries can give you a bit more insight into object structure.. https://github.com/NeilMacMullen/Textrude/blob/main/doc/format_conversion.md |
Beta Was this translation helpful? Give feedback.
-
Your altfilter function is returning an array instead of an object which is why the output shows square brackets which is why you're seeing those results.
No - this is not the case. Scriban, in common with most languages (including Javascript) has a strong distinction between primitive types (int, string, bool) and "objects" and "arrays" which are collections/containers of primitives. Objects are passed around by reference so changing the value of one of the primitives inside an object changes that value for everyone else who is using the object reference.. To put this in concrete terms, if you set model[0].Name to "foo", that change affects everyone "globally". It's tricky to do a "deep clone" of a structured object (this is true in all referenced based languages) but you can try to make your own copy in textrude by serialising to JSON/YAML then deserializing back again. Unfortunately this may throw away some type information since you are limited by the type representation available in the serialisation format. Anyway, the key takeaway is that if you are trying to start with the model and build "intermediate" calculated values you need to be careful not to overwrite any of the original model values - the good news is that you can just add a new property on an object just by assigning it...
|
Beta Was this translation helpful? Give feedback.
-
Good to hear - have a good holiday :-) |
Beta Was this translation helpful? Give feedback.
You may also find the debug.dump method useful for tracing changes to objects - see https://github.com/NeilMacMullen/Textrude/blob/main/doc/builtIns.md
Also the in-built format conversion libraries can give you a bit more insight into object structure..
https://github.com/NeilMacMullen/Textrude/blob/main/doc/format_conversion.md