“So greifen Sie in JavaScript zu Array von Objekten zu” Code-Antworten

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

So greifen Sie in JavaScript zu Array von Objekten zu

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

Arrays in Array von Objekten

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

So greifen Sie in JavaScript zu Array von Objekten zu

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

So greifen Sie in JavaScript zu Array von Objekten zu

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

Ähnliche Antworten wie “So greifen Sie in JavaScript zu Array von Objekten zu”

Fragen ähnlich wie “So greifen Sie in JavaScript zu Array von Objekten zu”

Weitere verwandte Antworten zu “So greifen Sie in JavaScript zu Array von Objekten zu” auf JavaScript

Durchsuchen Sie beliebte Code-Antworten nach Sprache

Durchsuchen Sie andere Codesprachen