-1

Is there a way I can do this in javascript?

events_json = events.json()
for detect in events_json.get("detections"):
    print(detect["source_location"])

This didn't seem to work in javascript:

for (let i = 0; i < data.length; i++) {
        console.log(JSON.stringify(data[detections][i][source_location]) 

Any suggestions to get this working in javascript? I tried following this link but wasn't able to understand how to use it for this: What is the Javascript equivalent of Python's get method for dictionaries

2
  • Can you give an example of the data you're using, and add that to the question? It'll help us debug the problem. Commented Aug 31, 2021 at 1:26
  • "I tried following this link but wasn't able to understand how to use it for this" Then I don't understand how we're supposed to help you; other people on Stack Overflow already gave their best shot at explaining the concept. Commented Aug 31, 2021 at 1:34

1 Answer 1

0

You need to quote detections and source_location so they'll be literals, not variables, just like you do in Python. You can also use . notation in JavaScript for property name literals.

data.detections.forEach(detect => console.log(detect.source_location));
Sign up to request clarification or add additional context in comments.

4 Comments

I tried using that and that wont work for adding multiple commands. The => seems to stop everything else afterwards: data.detections.forEach(detect => console.log(detect.source_location) let text = "<b>Device: <b>" + detect.machine_name + "<br>"; let pre = document.createElement('p'); pre.innerHTML = text; console.log(text) pre.style.cssText += 'font-size:16px;font-weight:bold; width: 350px; word-break: break-word;' output.appendChild(pre); ); I think this is a start so I will google how to use this without the => and that should fix everything
If the function body is more than one expression you have to put it in {}.
detect => { statement1; statement2; ...}
I was actually going to say that I learned how to fix it by googleing it and found out that I need {} after the =>. w3schools.com/js/js_arrow_function.asp Then I found out that you were kind enough to answer as well. lol Thanks for helping me out here.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.