M
M
Mykhailo2018-11-26 16:25:25
JavaScript
Mykhailo, 2018-11-26 16:25:25

How to output all name from objects in array (jsonplaceholder)?

There is a link:

fetch('https://jsonplaceholder.typicode.com/users')
  .then(response => response.json())
  .then(json => console.log(json))

which outputs 10 random numbers to the console. users with names, emails, etc.
How to display, for example, the names name: "..." of all 10 users?
So far, it was possible to display information only about the first user
import React, { Component } from 'react';
import './App.css';

function req1() {
  fetch('https://jsonplaceholder.typicode.com/users/1')
    .then(response => response.json())
    .then(json => {
      var name = json.name
      var body = json.username;
      var email = json.email;
      var website = json.website;
      document.getElementById("printUser").innerHTML = 
      '<td class="firstCol">1.</td>' + '<td>' + name + '</td>' 
      + '<td>' + body + '</td>' 
      + '<td>' + email + '</td>' 
      + '<td>' + website + '</td>';
      
    });
}

req1();


class App extends Component {
  render() {
    return(
      <div className="App">
      <table>
        <tbody>
          <th>json users</th>
          <tr id="printUser">
          </tr>
        </tbody>
      </table>
      </div>
    )
  }
}

export default App;

Answer the question

In order to leave comments, you need to log in

2 answer(s)
0
0xD34F, 2018-11-26
@msvystun

class App extends Component {
  state = {
    users: []
  }

  componentDidMount() {
    fetch('https://jsonplaceholder.typicode.com/users')
      .then(response => response.json())
      .then(users => this.setState({ users }));
  }

  render() {
    return(
      <div className="App">
        <table>
          <tbody>
          {this.state.users.map(n => (
            <tr key={n.id}>
              <td>{n.name}</td>
              <td>{n.username}</td>
              <td>{n.email}</td>
              <td>{n.website}</td>
            </tr>
          ))}
          </tbody>
        </table>
      </div>
    )
  }
}

A
Anton Spirin, 2018-11-26
@rockon404

I recommend that before you start writing anything, it is good to study the possibilities of the library or at least read the Tutorial and the Main Concepts and Advanced Guides sections of the official documentation .
What you want to implement correctly is done like this .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question