@sdetweil said in Config option with array of multiple values?:
generally when you expand the number of items in an array to objects ({}) you start to get thinking about what happens if you decide to add another entry in the object…
so in javascript you can NAME the elements
validSenders: [
{ url:"mom@example.com", name:"Mom", color:"#ff0000" },
{ url"dad@example.com", name:"Dad", color:"#00ff00" },
{ url:"son@example.com", name:"Son", color:"#0000ff" },
]
that way the code is not sensitive to the order of the elements
validSenders.forEach(sender =>{
if (sender.name =="Dad") {
do_something(sender.url)
}
})
So in this scenario, you have validSenders the array and, for example, validSenders.url as one attribute(?) of the array? In your example, where does “sender” come from? It’s not one of the named elements. Is it a sort of temporary variable that gets handed the array … object(?) … so in the first iteration of the forEach (which I assume loops through each entry in the validSenders array), sender would contain(?) url:"mom@example.com", name:“Mom”, and color:“#ff0000”? And sender.url would be “mom@example.com”?
That makes sense. I apologize for not knowing the terminology; I’m very new to the whole OOP sort of thing.
you can also use the array.filter() function
let selected_sender = validSenders.filter(sender=>{
if(sender.url==mailObj.sender[0].address)
return true
else
return false
})
if(selected_sender.length>0){
// we found a matching sender
}
the filter function passes each element array in turn to the function
if you want the element in the output array return true,
if not return false
Could this return false if it doesn’t match but an index if it matches so that one could refer to validSenders.color[selected_sender] to get the right attribute?
I really kinda wish I had a spare RPi laying around so I could do this testing on something other than my production mirror. (I mean, everyone’s aware that it’s a work-in-progress but I don’t want to muck it up.)
- that.config.validSenders.includes(mailObj.sender[0].address)
- daysAgo >= 0
- daysAgo <= that.config.daysToDisplay
the last two CANNOT be true at the same time
I’m not sure I understand… if daysToDisplay is, say, 50 and daysAgo = 25, then the last two would both be true?
What I’m trying to do is make sure that the sender of an e-mail is in the list of valid senders and that the date the e-mail was sent is not more than daysToDisplay days ago.
maybe what you wanted was
if (
that.config.validSenders.includes(mailObj.sender[0].address) &&
(daysAgo >= 0 && daysAgo <= that.config.daysToDisplay)
) {
this is two outer compares (with one inner)
I thought I tried that but even so, I don’t understand how that’s different from what I had. I thought that “If A && B && C” would be the same as “If A && (B && C)” – in either case, all three have to be true for the whole thing to be true.