1

I have a React class, which takes in a JSON array and should produce a set of divs:

import React, { Component } from "react";
import "./Maps.css";
import df3 from "./data/df3.json"
import sample from "./data/sample.json"


class Maps extends Component {
  constructor() {
    super();
    const data = df3;
    this.state = data
  }

  render()

  {
    var df4 = df3["ds"]
    var d1 = '';
    for (var key in df4)
    {
       var host = df4[key];
       d1 += '<div class="'+key+'" value="'+host[0]+'"/>';
    }

    return (

      <div id="Maps">

         </div>

    )
  }
}

export default Maps

So far, it is not returning the set of divs.

Here is the JSON:

{"ds":[{
  "s1": {
    "0": 0.0,
    "1": 2.0,
    "2": 0.0,
}
  "s1": {
    "0": 0.0,
    "1": 2.0,
    "2": 0.0,
}
  "s3": {
    "0": 2.0,
    "1": 2.0,
    "2": 0.0,
}}]}

which, when fed to the class, should output the following divs:

<div class="s1" value="0"/><div class="s2" value="0"/><div class="s3" value="2"/>

but I'm not sure how to return it, and when i console log d1, i get:

<div class="0" value="undefined"/>

any ideas, as always, are helpful!

3
  • 1
    You need to use the map instant of for loop. Commented May 20, 2020 at 18:14
  • could you explain a little more? Commented May 20, 2020 at 18:15
  • In addition to that, once d1 holds the array of divs, you need to put {d1} in the rendered output Commented May 20, 2020 at 18:16

1 Answer 1

2

Your render method does not return anything but an empty div.

A correct example would be:

render() {
    const myArray = df3["ds"];
    return myArray.map(item => (
        Object.keys(item).map(key => (
            <div key={key} className={key} value={item[key]["0"]} /> 
        )
    ))
}
Sign up to request clarification or add additional context in comments.

3 Comments

what i really want is the first value in the sub nodes. s1 should have a value of 0, s2 should have value of 0, s3 should have value of 2
Just updated my comment. It should be now what you are looking for!
Each "key" is equal to "s1", "s2", "s3" Each item[key]["0"] is equal to 0.0, 0.0, 2.0

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.