js object array code example
Example 1: list javascript
//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)
Example 2: 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]);
}
Example 3: array of objects javascript
var widgetTemplats = [
{
name: 'compass',
LocX: 35,
LocY: 312
},
{
name: 'another',
LocX: 52,
LocY: 32
}
]
Example 4: javascript array read object value in array
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);
Example 5: return array javascript
function func() {
return [5, "string", {a: 7}];
}
Example 6: array from js
//Array.from() lets you create Arrays from array-like objects
//(objects with a length property and indexed elements);
//and also:
//More clearly, Array.from(obj, mapFn, thisArg)
//has the same result as Array.from(obj).map(mapFn, thisArg),
//except that it does not create an intermediate array.
//Basically, it's a declaration that overrides the length property of the method
//(so that it has to be used with the same name length),
//setting it with the same value of the given variable.
//The values are still undefined, it's just a different notation. Take a look:
console.log(Array.from(length, (_,i) => i));
// It doesn't works with non-iterables
// In this case we are passing an integer
console.log(Array.from({LENGTH}, (_,i) => i));
// It doesn't work with a property name different from "length"
console.log(Array.from({length}, (_,i) => i));
// It works because overrides the .length property of the array
// The method Array.from() assumes that the property...
// ...is referring to an iterable (also if not declared)
console.log(Array.from(Array(length), (_,i) => i));
// This is the demonstration of the above assertion
// In this case we are using a declared array through...
// ...an instance of the straight method Array()...
// ...that accepts an integer as value
//in case any one reads this a got this from er0s in edabit