Does anyone know of a simple way to merge two or more lists without removing duplicates?
The ‘merged with’ function removes duplicates. The lists are ingredients, which are linked to a recipe, of which there could be two or more in total FYI.
To give a little more context, I need the final list of ingredients to run through ListItemExpression with expression JSON.stringify(value()) to create an output, which is used for a JS SDK.
One option might be to create individual strings from each recipe’s ingredients (i.e. use one ListItemExpression per recipe) and add them individually in the JS, though this seems pretty clunky.
I think you might be thinking about data types in Bubble incorrectly. It sounds to me like, in your app, an ingredient should not just be represented by a text string. An Ingredient should be an object (a data type). It should have at least one field for Name (that is a text). It might also have a Category and other descriptors.
A Recipe data type then has Ingredients (a list of type Ingredient). When Recipes are represented as a list (for example, merging Recipe 1:make list and Recipe 2: make list or by constructing the list using add item, searches, etc.) that List_of_Recipe’s Ingredient’s Name will be a list of of type text.
Those texts will not be de-duped (though they can be de-duped by appending the :unique items operator either after Ingredient or after Ingredient’s Name).
The above would be the easiest way (in the long run) to manage all of this.
It is much harder to do this backward at runtime. Say you have two lists of texts (which is what you seem to have) and you need to merge them. You can’t do that with :merge as that creates a de-duped list. You could do it in a page using JavaScript (loop over the lists of texts and push each item into an array; in this way you could construct a list with duplicates). This has limitations (you have to do it in the page, you can’t easily do it on the server side).
Can you show an example of what you want the output to look like? Is it just ingredients, or other attributes such as quantity, package type?
If its just a list of texts, and if a recipe field “ingredients” is a list of texts, you can likely do your merge first on list of recipes, then just send the output to a Run Javascript or Expression with something like this, note the particular characters …
The output will need to be something like this: “600 g ripe mixed-colour cherry tomatoes”, “2 heaped teaspoons wholegrain mustard”, “120 ml Worcestershire sauce”, “2 onions”, “500 g carrots”, “900 g shin of beef , bone out (ask your butcher for the bone)”…
It is a list of texts (I believe). The quantities are just included in the text rather than a separate field and the JS SDK is able to pick this up with reasonable accuracy.
Here is a sample of the recipe data. I basically want to combine the lists in the ‘ingredient’ field into one long list, without losing duplicates, then run it through ListItemExpression (this part is working).
@keith I agree I probably need to revisit the data model. Ideally, I’d like to get a prototype up and running and try using it for a few weeks before making significant changes.
Sorry, I didn’t specify… The total list of ingredients will be made up of the recipes that a user selects (between two and seven recipes). The recipes are displayed in individual Groups so I guess a search would have to include Group #1’s Recipe’s ingredients joined with Group #2’s Recipe’s ingredients… up to Group #n’s Recipe’s ingredients, if that makes sense?
That’s what I’m struggling with, without using the merge function.
The more I think about it, the more I’m thinking the list of ingredients should be temporarily stored in a table or something and the search can be against that list.