Simone
03/04/2022, 1:02 PMfor (d in posts) {
for (key in d) {
structkeyexists(key,'views');
dump(key);
}
}
Tim
03/04/2022, 1:08 PMAdam Cameron
structKeyExists
take?
• And what are you giving it?
• Does that seem right?
• What are you expecting the statement structkeyexists(key,'views')
, by itself, to do?Simone
03/04/2022, 2:42 PMTim
03/04/2022, 3:30 PMd
is a struct and d
has a views
key.
I'm just going to guess at your data structure here, because you still haven't told us... And that posts is an array of structs, like so:
var posts = [
{
subject: "Post 1",
content: "This is the first post",
views: 14
},
{
subject: "Post 2",
content: "Post 1 is stupid. I disagree.",
views: 6
}
];
Then, your code would be
for (d in posts) {
for (key in d) {
structkeyexists(key, "views");
dump(key);
}
}
And processing it would look like this:
1. posts has 2 items, so it enters the loop.
2. d = the first struct (post 1)
3. d has keys, so it enters the second loop.
4. key = "subject"
5. structkeyexists("subject", "views") throws an error because "subject" is not a struct. It's a string.