logo

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ
No Result
View All Result
logo
No Result
View All Result
Trang chủ how to get the first element of an array?

How To Get The First Element Of An Array?

by Admin _ January 12, 2023
Get First Array Element Using find() in JavaScriptGet First Array Element Using an array in JavaScriptGet First Array Element Using shift() in JavaScriptGet First Array Element Using filter() in JavaScriptGet First Array Element Using Destructuring Assignment in JavaScript
*

Arrays are an essential part of any programming language because it holds many elements in an ordering manner. All these elements in them are accessed via an index. In JavaScript, arrays are regular objects that hold the value on the specific key, a numeric key. This article will introduce how khổng lồ get the first element of an array in JavaScript.

Bạn đang xem: How to get the first element of an array?

JavaScript provides various methods to lớn get the first element. Let’s learn those methods.

Get First Array Element Using find() in JavaScript

find() is an in-built array method provided by JavaScript, which finds the elements whose value matches the specified condition.

Syntax of find() in JavaScript


Parameters

$condition: It is a mandatory parameter. The user can pass any condition related khổng lồ the array element, và it will try to lớn find out the first matching element which satisfies the condition.

Return Value

Return the first value which matches with a given condition. If no condition matches, it will return undefined.Example code:
const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.find(element => element != undefined);console.log(firstElement);

Get First Array Element Using an array in JavaScript

As discussed earlier, each array element has a quality index assigned khổng lồ it. The numeric key/index inside an array starts with 0. Using this index, you can retrieve the first element of an array in JavaScript.

Syntax of array in JavaScript


Parameters

$index: It is a mandatory parameter that accepts only integer value that specifies the index of the element to be retrieved.

Xem thêm: Bệnh Ung Thư Tai Trong - Chẩn Đoán Và Điều Trị Ung Thư Tai

Return Value

It returns the element whose index is specified.Example code:
const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements<0>;console.log(firstElement);

Get First Array Element Using shift() in JavaScript

It is an in-built array method provided by JavaScript, which removes the first element of an array and returns the removed element. The only problem with this array method is that it mutates/modifies the original array.

Syntax of shift() in JavaScript


Return Value

It returns the first element of the array, which is removed from the original array. If an array is an empty array, it will return undefined.Example code:
const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.shift(0);console.log(firstElement);console.log(arrayElements);

Get First Array Element Using filter() in JavaScript

It is a JavaScript in-built array method that filters out all the elements which satisfy the given condition. It will create a copy of the matching elements array. The sole difference between find() & filter() is that first stops the traversing as soon as the first matching element is found while later will continue till the last element of an array. It is less efficient if an array form size is larger because of the traversal property.

Syntax of filter() in JavaScript


Parameters

$condition: It is a mandatory parameter. The user can pass any condition related lớn the array element, và it will try khổng lồ find out all matching element that satisfies the condition.

Return Value

Return the new array containing elements that match with a given condition.Example code:
const arrayElements = <5, 10, 0, 20, 45>;const firstElement = arrayElements.filter(element => element!= undefined).shift();console.log(firstElement);console.log(arrayElements);

Get First Array Element Using Destructuring Assignment in JavaScript

Destructing assignments is a very powerful syntax available in JavaScript which allows unpacking the elements of array or properties of an object lớn be geted into distinct variables.

Syntax of Destructuring Assignment in JavaScript


Return Value

It returns the new variable through which you can access the value of an array or object.Example code:
const arrayElements = <5, 10, 0, 20, 45>;let = arrayElements;console.log(firstElement);
Output:
5
Author: Shraddha Paghdar

Shraddha is a JavaScript nerd that utilises it for everything from experimenting lớn assisting individuals và businesses with day-to-day operations & business growth. She is a writer, chef, và computer programmer. As a senior MEAN/MERN stack developer and project manager with more than 4 years of experience in this sector, she now handles multiple projects. She has been producing technical writing for at least a year and a half. She enjoys coming up with fresh, innovative ideas.

LinkedIn

Related Article - JavaScript Array

Check if Array Contains Value in JavaScriptCreate Array of Specific Length in JavaScriptConvert Array lớn String in JavaScriptRemove First Element From an Array in JavaScriptSearch Objects From an Array in JavaScriptConvert Arguments to lớn an Array in JavaScriptEzoicreport this ad
Share Tweet Linkedin Pinterest
Previous Post

Express js upload and save base64 image into file system

Next Post

Javascript datetime to string format dd/mm/yyyy

CÙNG CHUYÊN MỤC

lấy giá trị của option trong javascript

Lấy giá trị của option trong javascript

28/03/2021
nhập dữ liệu từ bàn phím trong javascript

Nhập dữ liệu từ bàn phím trong javascript

28/04/2021
lấy giá trị từ input trong javascript

Lấy giá trị từ input trong javascript

28/04/2021
download javascript for windows 10

Download javascript for windows 10

28/04/2021
bài dịch head first design patterns tiếng việt

Bài dịch head first design patterns tiếng việt

25/01/2023
how to get the first element of an array?

How to get the first element of an array?

12/01/2023
tip: you can get the value of an input element as a number

Tip: you can get the value of an input element as a number

05/01/2023
tìm ước chung lớn nhất javascript

Tìm ước chung lớn nhất javascript

29/12/2022

Newsletter

The most important automotive news and events of the day

We won't spam you. Pinky swear.

Chuyên Mục

  • Hỏi Đáp
  • Kiến Thức
  • Sức Khỏe
  • Tử Vi
  • Công Nghệ

News Post

  • Phần mềm tải video

About

Chúng tôi tạo ra trang web nhằm mục đích mang lại kiến thức bổ ích cho cộng đồng, các bài viết được sưu tầm từ nhiều nguồn trên internet giúp mang lại kiến thức khách quan dành cho bạn

©2023 darkedeneurope.com - Website WordPress vì mục đích cộng đồng

Liên Hệ - Giới Thiệu - Nội Quy - Bảo Mật

No Result
View All Result
  • Trang chủ
  • Chuyên mục
    • Hỏi Đáp
    • Kiến Thức
    • Sức Khỏe
    • Tử Vi
    • Công Nghệ
  • Lưu trữ
  • Liên hệ

© 2023 darkedeneurope.com - Website WordPress vì mục đích cộng đồng.