“Comment accéder à la table d'objets en javascript” Réponses codées

tableau javascript

//create an array like so:
var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    console.log(colors[i]);
}
Grepper

Comment accéder à la table d'objets en javascript

var events = [
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 1
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 2
  },
  {
    userId: 1,
    place: "Wormholes Allow Information to Escape Black Holes",
    name: "Check out this recent discovery about workholes",
    date: "2020-06-26T17:58:57.776Z",
    id: 3
  }
];
console.log(events[0].place);
Dead Dingo

tableaux à l'intérieur de la table d'objets

var response = {data: [{users: [1,2,3]}, {users: [4,5,6]}]}

var users = response.data.map(o => o.users)

const usersCollection = [].concat(...users)

console.log(usersCollection)
Worrisome Warbler

Comment accéder à la table d'objets en javascript

async function getData() {
   const response = await fetch('https://jsonplaceholder.typicode.com/users');
            const data = await response.json();
            const {
                id = data[1]['id'],
                name = data[1]['name'],
                username = data[1]['username'],
                email = data[1]['email'],
                address = data[1]['address']
            } = data
  			console.log(id)
            console.log(name)
            console.log(email)
            console.log(username)
            console.log(address)
        }
        getData();
}
Anthony Smith

Comment accéder à la table d'objets en javascript

Accessing nested data structures
A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation.

Here is an example:

const data = {
    code: 42,
    items: [{
        id: 1,
        name: 'foo'
    }, {
        id: 2,
        name: 'bar'
    }]
};
Let's assume we want to access the name of the second item.

Here is how we can do it step-by-step:

As we can see data is an object, hence we can access its properties using dot notation. The items property is accessed as follows:

data.items
The value is an array, to access its second element, we have to use bracket notation:

data.items[1]
This value is an object and we use dot notation again to access the name property. So we eventually get:

const item_name = data.items[1].name;
Alternatively, we could have used bracket notation for any of the properties, especially if the name contained characters that would have made it invalid for dot notation usage:

const item_name = data['items'][1]['name'];
Healthy Hare

Réponses similaires à “Comment accéder à la table d'objets en javascript”

Questions similaires à “Comment accéder à la table d'objets en javascript”

Plus de réponses similaires à “Comment accéder à la table d'objets en javascript” dans JavaScript

Parcourir les réponses de code populaires par langue

Parcourir d'autres langages de code