Hi,
for my wife I created a shopping list ....
I save the value (name - true/false) into a file txt ...
and I load this values into more variables with this code (this is the best I got)
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?
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 ```
)
```
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
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
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.
1 Like
Oh yesss ..
Thank you @TotallyInformation , thank you very much !
@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)
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?
you would need to share some example data in a form that allows copying into an example.
No problem, I'll use my old code. Thank you again ....
That hard work ...... (for my skills of course)
const x = msg.payload;
x.forEach(element => {
let prodotto
prodotto = ((element.col2) === "true") ? true : false
flow.set((element.col1), prodotto);
});
1 Like
system
Closed
1 September 2022 04:56
10
This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.