“Como acessar a matriz de objetos em JavaScript” Respostas de código

JavaScript Array

//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

Como acessar a matriz de objetos em 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

Matrizes dentro da matriz de objetos

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

Como acessar a matriz de objetos em 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

Como acessar a matriz de objetos em 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

Respostas semelhantes a “Como acessar a matriz de objetos em JavaScript”

Perguntas semelhantes a “Como acessar a matriz de objetos em JavaScript”

Mais respostas relacionadas para “Como acessar a matriz de objetos em JavaScript” em JavaScript

Procure respostas de código populares por idioma

Procurar outros idiomas de código