Answers for "javascript arry"

140

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]);
}
Posted by: Guest on July-01-2019
-2

javascript array

window.location.href
Posted by: Guest on December-12-2020
5

array javascript

//Their are no lists in javascript, they are called arays,wich are basically the same thing

var fruits = ['Apple', 'Banana'];

console.log(fruits.length);
// 2
var first = fruits[0];
// Apple

var last = fruits[fruits.length - 1];
// Banana

fruits.forEach(function(item, index, array) {
  console.log(item, index);
});
// Apple 0
// Banana 1

var newLength = fruits.push('Orange');
// ["Apple", "Banana", "Orange"]

var last = fruits.pop(); // deletes orange
// ["Apple", "Banana"];

var first = fruits.shift(); // supprime Apple (au début)
// ["Banana"];

var newLength = fruits.unshift('Strawberry') // adds to the start
// ["Strawberry", "Banana"];

fruits.push('Mango');
// ["Strawberry", "Banana", "Mango"]

var pos = fruits.indexOf('Banana');
// 1

var removedItem = fruits.splice(pos, 1); // deletes on element at the position pos
//removes one element at the position pos
// ["Strawberry", "Mango"]




var vegetables = ['Cabbage', 'Turnip', 'Radish', 'Carrot'];
console.log(vegetables);
// ["Cabbage", "Turnip", "Radish", "Carrot"]

var pos = 1, n = 2;

var removedItems = vegetables.splice(pos, n);
// n defines the number of element to delete starting from pos

console.log(vegetables);
// ["Cabbage", "Carrot"] 
console.log(removedItems);
// ["Turnip", "Radish"] (splice returns list of deleted objects)
Posted by: Guest on February-18-2021
0

array in javascript

var colors = ["red","blue","green"];

//you can loop through an array like this:
for (var i = 0; i < colors.length; i++) {
    document.write(colors[i]);//printing elements of the array 
	}
	colors.push("pink");//adds element to array;
	colors.pop(); // removes last element of the array 
	colors.shift("purple","aqua");//adds one or more items at the front of the array 
	color.reverse();//reverses the array 
	var g=color.indexOf("green");//returns index of value
	var c= color.includes("red");//to search for an element
	color.slice();//splits the array
	color.sort() ; //sorts the array
Posted by: Guest on July-27-2021
4

javascript array

const arr = [1, 2, 3];
Posted by: Guest on October-04-2020
1

functions in arrays javascript

var argsContainer = ['hello', 'you', 'there'];
var functionsContainer = [];

for (var i = 0; i < argsContainer.length; i++) {
var currentArg = argsContainer[i]; 

  functionsContainer.push(function(currentArg){
    console.log(currentArg);
  });
};

for (var i = 0; i < functionsContainer.length; i++) {
  functionsContainer[i](argsContainer[i]);
}
Posted by: Guest on July-29-2020

Code answers related to "Javascript"

Browse Popular Code Answers by Language