# Load variables for shopping list

**URL:** https://discourse.nodered.org/t/load-variables-for-shopping-list/66541
**Category:** General
**Created:** [17 August 2022 09:14 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541 "2022-08-17T09:14:22Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [17 August 2022 09:14 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/1 "2022-08-17T09:14:23Z")

</div>

Hi,  
for my wife I created a shopping list ....

I save the value (name - true/false) into a file txt ...

![listatxt](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/9/1/916c36668ef3164aeceb683073852fd228279ad6.png)

and I load this values into more variables with this code (this is the best I got)

 ![loadvar](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/2/f/2fc57e3a22beeaa8824cbb88f48e18c2cac1b1ac.png)

It's works fine from many months but the "problem" is when I want to add (or delete) one object of the list, because I am forced to renumber all the objects as it's written the code ....

Example: if I delete the object "Arance", the next object "Avocado" becomes x[3] and not more x[4] and all the next consequently and they are about 90 ...

How I can improve the code to avoid the renumbering?

---

<div class="post-metadata">

### Author: ![zenofmud](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/zenofmud/32/316_2.png) [@zenofmud](https://discourse.nodered.org/u/zenofmud)
#### Post date: [17 August 2022 09:56 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/2 "2022-08-17T09:56:01Z")

</div>

It would help if you provided the complete flow. There may be another way to do what you want.

In order to make code readable and usable it is necessary to surround your code with three backticks (also known as a left quote or backquote `````)

````auto
``` 
   code goes here 
```

````

You can edit and correct your post by clicking the pencil ✏ icon.

See this post for more details - [How to share code or flow json](https://discourse.nodered.org/t/how-to-share-code-or-flow-json/506)

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 August 2022 11:55 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/3 "2022-08-17T11:55:27Z")

</div>

> [@Giamma](#):
>
> x[3] and not more x[4]

Rule number 1 of computing - let the computer do the work 🙂

You have an array of data so you can walk the array rather than trying to copy/paste many times.

There are various ways to do that. I tend to use something like

```auto
const x = msg.payload
let p = flow.get( 'prodotto' ) || {}

x.forEach( product => {
    p[product.name] = product.state 
} )

flow.set( 'prodotto', p )

...

```

Obviously you may have to play with things to match your data.

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [17 August 2022 12:19 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/4 "2022-08-17T12:19:49Z")

</div>

Oh yesss ..  
Thank you @TotallyInformation , thank you very much !

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [17 August 2022 14:31 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/5 "2022-08-17T14:31:59Z")

</div>

@TotallyInformation sorry ..... don't work

'prodotto' is not the name of the variable, prodotto is the value (true/false) of every variable with the name Aceto, Affettati, Albicocche ...

Into array (see txt) I have col1 (the name) and col2 (the value)

 ![array](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/d/b/dbde82e7f61445f16498537e9155ab8a07c49448.png)

I'd like to read the array (x) and for every object set a variable with name col1 value  
and value col2 value

How can I modify your code?

---

<div class="post-metadata">

### Author: ![TotallyInformation](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/totallyinformation/32/31_2.png) [@TotallyInformation](https://discourse.nodered.org/u/TotallyInformation)
#### Post date: [17 August 2022 15:59 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/6 "2022-08-17T15:59:46Z")

</div>

you would need to share some example data in a form that allows copying into an example.

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [17 August 2022 16:18 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/7 "2022-08-17T16:18:19Z")

</div>

No problem, I'll use my old code. Thank you again ....

---

<div class="post-metadata">

### Author: ![Giamma](https://avatars.discourse-cdn.com/v4/letter/g/e47774/32.png) [@Giamma](https://discourse.nodered.org/u/Giamma)
#### Post date: [18 August 2022 04:56 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/9 "2022-08-18T04:56:31Z")

</div>

That hard work ...... (for my skills of course)

```auto
const x = msg.payload;
x.forEach(element => {
    let prodotto
    prodotto = ((element.col2) === "true") ? true : false
    flow.set((element.col1), prodotto);
});

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)
#### Post date: [1 September 2022 04:56 UTC](https://discourse.nodered.org/t/load-variables-for-shopping-list/66541/10 "2022-09-01T04:56:35Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
