0
Q:

list 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]);
}
69
//Creating a list
var fruits = ["Apple", "Banana", "Cherry"];

//Creating an empty list
var books = [];

//Getting element of list
//Lists are ordered using indexes
//The first element in an list has an index of 0, 
//the second an index of 1,
//the third an index of 2,
//and so on.
console.log(fruits[0]) //This prints the first element 
//in the list fruits, which in this case is "Apple"

//Adding to lists
fruits.push("Orange") //This will add orange to the end of the list "fruits"
fruits[1]="Blueberry" //The second element will be changed to Blueberry

//Removing from lists
fruits.splice(0, 1) //This will remove the first element,
//in this case Apple, from the list
fruits.splice(0, 2) //Will remove first and second element
//Splice goes from index of first argument to index of second argument (excluding second argument)
11
let fruits = ['Apple', 'Banana']

console.log(fruits.length)
// 2
6
var fruits = ['Apple', 'Banana'];

console.log(fruits.length);
// 2
6

var cars = [

  "Saab",

    "Volvo",

    "BMW"

]; 
4
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);
2
//create array
var things = ["banana", "car", "computer"];
//access array(starts at 0)
things[0]
//iterate through an array
for (var i = 0; i < things.length; i++) {
  things[i]
}
3

var cars = ["Saab", "Volvo", "BMW"];
 
2

New to Communities?

Join the community