xxxxxxxxxx
var items = ["Item 1", "Item 2", "Item 3"];
var firstItem = typeof items[0] !== "undefined" ? items[0] : null;
console.log(firstItem);
xxxxxxxxxx
const list = ['apple', 'banana', 'orange', 'strawberry']
const size = 3
const items = list.slice(0, size) // res: ['apple', 'banana', 'orange']
xxxxxxxxxx
let array = [1,2,3] // makes your array
array[0] // returns first element of your array.
xxxxxxxxxx
data = [{
id: 1,
name: "Pedro"
}, {
id: 2,
name: "Pedro"
}, {
id: 3,
name: "Pedro"
}]
data.find(item => item.name == "Pedro") // Return { id: 1, name: "Pedro" }
data.findLast(item => item.name == "Pedro") // Return { id: 3, name: "Pedro" }
xxxxxxxxxx
// Assuming the array is already defined
let array = [1, 2, 3, 4, 5];
// Method 1: Using array indexing
let firstElement = array[0];
console.log(firstElement); // Output: 1
// Method 2: Using Array.prototype.shift()
let shiftedElement = array.shift();
console.log(shiftedElement); // Output: 1
console.log(array); // Output: [2, 3, 4, 5]
xxxxxxxxxx
let mylist = ['one','two','three','last'];
mylist[0],mylist[1],mylist[2],mylist[3];//this is called indexing and slicing in python
//in javascript it called getting elements in array
xxxxxxxxxx
const array = [2, 4, 6, 8, 2, 4, 6, 8];
// Define the value to search for
const searchValue = 6;
// Find the first occurrence of the value in the array
const index = array.indexOf(searchValue);
if (index > -1) {
console.log(`The first occurrence of ${searchValue} is at index ${index}`);
} else {
console.log(`${searchValue} was not found in the array`);
}
xxxxxxxxxx
let array = ["a", "b", "c", "d", "e"];
// Both first1 and first2 have the same value.
let [first1] = array;
let first2 = array[0];