Objek. metode keys() digunakan untuk mengembalikan nama properti objek sebagai array. Properti length digunakan untuk mendapatkan jumlah kunci yang ada di objek. Ini memberikan panjang objek.
Sintaksis
objectLength = Object.keys(exampleObject).lengthContoh.
html
<!DOCTYPE html>
<html>
<head>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;2var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;3var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;2>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6head>
<<!DOCTYPE html>1>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<<!DOCTYPE html>5 <!DOCTYPE html>6<!DOCTYPE html>7<!DOCTYPE html>8<!DOCTYPE html>9<!DOCTYPE html>5>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<<5var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;3<5>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
html4html5
html6html7
html6html9
html6>1
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_91_______3
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
html4 4
html4 6
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_89_______7
________90______4<head0 head1<!DOCTYPE html>7head3head4head0>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<>4 >5<!DOCTYPE html>7>7>
html_4var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;00
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6>4>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;08>
html_4var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;11
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_12
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;14
html6html5
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17html7
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17html9
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17>1
html6>3
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;27
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;29
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;32
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_33var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;34
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_91_______3
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;08>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6<!DOCTYPE html>1>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6htmlvar key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;47
Keluaran
Temukan panjang objek JavaScript
Metode 2. Ulangi semua bidang objek dan periksa propertinya
Metode hasOwnProperty() digunakan untuk mengembalikan nilai boolean yang menunjukkan apakah objek memiliki properti yang ditentukan sebagai propertinya sendiri. Metode ini dapat digunakan untuk memeriksa apakah setiap kunci ada di objek itu sendiri. Isi objek dilingkarkan dan jika kuncinya ada, jumlah total kunci bertambah. Ini memberikan panjang objek.
Sintaksis
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_Contoh.
html
<!DOCTYPE html>
<html>
<head>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;2var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;3var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;2>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6head>
<<!DOCTYPE html>1>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<<!DOCTYPE html>5 <!DOCTYPE html>6<!DOCTYPE html>7<!DOCTYPE html>8<!DOCTYPE html>9<!DOCTYPE html>5>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<<5var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;3<5>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
html4html5
html6html7
html6html9
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;97
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;99
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_91_______3
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
html4 4
html4 6
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<html2>
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_89_______7
________90______4<head0 head1<!DOCTYPE html>7head3head4head0>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6html2>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<>4 >5<!DOCTYPE html>7>7>
html_4var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;00
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6>4>
<_2
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0<var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;08>
html_4var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;11
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;14
html6html5
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17html7
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17html9
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_17var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;97
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_17var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_99
html6>3
html6<!DOCTYPE html>77
html6<!DOCTYPE html>80
html6<!DOCTYPE html>82
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;17<!DOCTYPE html>84
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_33<!DOCTYPE html>87
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_33<!DOCTYPE html>89
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_33<!DOCTYPE html>91
html6>3
html6<!DOCTYPE html>95
html6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;32
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_33var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;34
________90var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;_91_______3
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;0var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;08>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6<!DOCTYPE html>1>
var key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;6htmlvar key, count = 0; // Check if every key has its own property for (key in exampleObject) { if (exampleObject.hasOwnProperty(key)) // If the key is found, add it to the total length count++; } objectLenght = count;47
Keluaran
Temukan panjang objek JavaScript
JavaScript terkenal untuk pengembangan halaman web tetapi juga digunakan di berbagai lingkungan non-browser. Anda dapat mempelajari JavaScript dari awal dengan mengikuti Tutorial JavaScript dan Contoh JavaScript ini