-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

  • Can you give an example of the data you're using, and add that to the question? It'll help us debug the problem. – Andy Aug 31 '21 at 01: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. – Karl Knechtel Aug 31 '21 at 01:34

1 Answers1

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));
Barmar
  • 741,623
  • 53
  • 500
  • 612
  • 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 = "Device: " + detect.machine_name + "
    "; 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
    –  Aug 31 '21 at 01:42
  • 1
    If the function body is more than one expression you have to put it in `{}`. – Barmar Aug 31 '21 at 01:44
  • 1
    `detect => { statement1; statement2; ...}` – Barmar Aug 31 '21 at 01:45
  • I was actually going to say that I learned how to fix it by googleing it and found out that I need {} after the =>. https://www.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. –  Aug 31 '21 at 01:46