Cara menggunakan object number javascript

Example 1: Count the Number of Key in an Object Using for...in

// program to count the number of keys/properties in an object

const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
};

let count = 0;

// loop through each key/value
for(let key in student) {

    // increase the count
    ++count;
}

console.log(count);

Output

Table of Contents

  • Example 1: Count the Number of Key in an Object Using for...in
  • Example 2: Count the Number of Key in an Object Using Object.key()
  • JavaScript Properties
  • Accessing JavaScript Properties
  • JavaScript for...in Loop
  • Adding New Properties
  • Deleting Properties
  • Nested Objects
  • Nested Arrays and Objects
  • Property Attributes
  • Prototype Properties
  • Can object properties be numbers JavaScript?
  • Can a JavaScript object key be a number?
  • Can object properties start with number?
  • Can an object property be a string?

3

The above program counts the number of keys/properties in an object using the for...in loop.

The count variable is initially 0. Then, the for...in loop increases the count by 1 for every key/value in an object.

Note: While using the for...in loop, it will also count inherited properties.

For example,

const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
};

const person = {
    gender: 'male'
}

student.__proto__ = person;

let count = 0;

for(let key in student) {

    // increase the count
    ++count;
}

console.log(count); // 4

If you only want to loop through the object's own property, you can use the hasOwnProperty() method.

if (student.hasOwnProperty(key)) {
    ++count:
}

Example 2: Count the Number of Key in an Object Using Object.key()

// program to count the number of keys/properties in an object

const student = { 
    name: 'John',
    age: 20,
    hobbies: ['reading', 'games', 'coding'],
};

// count the key/value
const result = Object.keys(student).length;

console.log(result);

Output

3

In the above program, the Object.keys() method and the length property are used to count the number of keys in an object.

The Object.keys() method returns an array of a given object's own enumerable property names i.e. ["name", "age", "hobbies"].

The length property returns the length of the array.


Properties are the most important part of any JavaScript object.


JavaScript Properties

Properties are the values associated with a JavaScript object.

A JavaScript object is a collection of unordered properties.

Properties can usually be changed, added, and deleted, but some are read only.


Accessing JavaScript Properties

The syntax for accessing the property of an object is:

objectName.property      // person.age

or

objectName["property"]   // person["age"]

or

objectName[expression]   // x = "age"; person[x]

The expression must evaluate to a property name.

Example 2

person["firstname"] + " is " + person["age"] + " years old.";

Try it Yourself »



JavaScript for...in Loop

The JavaScript for...in statement loops through the properties of an object.

Syntax

for (let variable in object) {
  // code to be executed
}

The block of code inside of the for...in loop will be executed once for each property.

Looping through the properties of an object:

Example

const person = {
  fname:" John",
  lname:" Doe",
  age: 25
};

for (let x in person) {
  txt += person[x];
}

Try it Yourself »


Adding New Properties

You can add new properties to an existing object by simply giving it a value.

Assume that the person object already exists - you can then give it new properties:


Deleting Properties

The delete keyword deletes a property from an object:

Example

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};

delete person.age;

Try it Yourself »

or delete person["age"];

Example

const person = {
  firstName: "John",
  lastName: "Doe",
  age: 50,
  eyeColor: "blue"
};

delete person["age"];

Try it Yourself »

The delete keyword deletes both the value of the property and the property itself.

After deletion, the property cannot be used before it is added back again.

The delete operator is designed to be used on object properties. It has no effect on variables or functions.

The delete operator should not be used on predefined JavaScript object properties. It can crash your application.


Nested Objects

Values in an object can be another object:

Example

myObj = {
  name:"John",
  age:30,
  cars: {
    car1:"Ford",
    car2:"BMW",
    car3:"Fiat"
  }
}

You can access nested objects using the dot notation or the bracket notation:

or:

or:

or:


Nested Arrays and Objects

Values in objects can be arrays, and values in arrays can be objects:

Example

const myObj = {
  name: "John",
  age: 30,
  cars: [
    {name:"Ford", models:["Fiesta", "Focus", "Mustang"]},
    {name:"BMW", models:["320", "X3", "X5"]},
    {name:"Fiat", models:["500", "Panda"]}
  ]
}

To access arrays inside arrays, use a for-in loop for each array:

Example

for (let i in myObj.cars) {
  x += "<h2>" + myObj.cars[i].name + "</h2>";
  for (let j in myObj.cars[i].models) {
    x += myObj.cars[i].models[j];
  }
}

Try it Yourself »


Property Attributes

All properties have a name. In addition they also have a value.

The value is one of the property's attributes.

Other attributes are: enumerable, configurable, and writable.

These attributes define how the property can be accessed (is it readable?, is it writable?)

In JavaScript, all attributes can be read, but only the value attribute can be changed (and only if the property is writable).

( ECMAScript 5 has methods for both getting and setting all property attributes)


Prototype Properties

JavaScript objects inherit the properties of their prototype.

The delete keyword does not delete inherited properties, but if you delete a prototype property, it will affect all objects inherited from the prototype.

Can object properties be numbers JavaScript?

According to the MDN JavaScript documentation you can define object literal property names using integers: Additionally, you can use a numeric or string literal for the name of a property.

Can a JavaScript object key be a number?

Each key in your JavaScript object must be a string, symbol, or number.

Can object properties start with number?

Illegal meaning that with dot notation, you're limited to using property names that are alphanumeric (plus the underscore _ and dollar sign $ ), and don't begin with a number.

Can an object property be a string?

Property keys must be strings or symbols (usually strings). Values can be of any type.