DEV Community

Cover image for JavaScript : Object Methods (English/Hindi)
Dharmik Dholu
Dharmik Dholu

Posted on

JavaScript : Object Methods (English/Hindi)

English

.keys():
This method returns an array of all the keys (property names) of an object.

// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const keys = Object.keys(person);
console.log(keys); // Output: ["name", "age", "city"]
Enter fullscreen mode Exit fullscreen mode

.values():
This method returns an array of all the values of an object.

// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const values = Object.values(person);
console.log(values); // Output: ["John", 30, "New York"]
Enter fullscreen mode Exit fullscreen mode

.entries():
This method returns an array of key-value pairs (as arrays) of an object.

// Example:
const person = {
  name: "John",
  age: 30,
  city: "New York"
};

const entries = Object.entries(person);
console.log(entries);
// Output: [["name", "John"], ["age", 30], ["city", "New York"]]
Enter fullscreen mode Exit fullscreen mode

.assign():
This method is used to copy the values of all enumerable properties from one or more source objects to a target object.

// Example:
const target = { a: 1, b: 2 };
const source = { b: 3, c: 4 };

const result = Object.assign(target, source);
console.log(target); // Output: { a: 1, b: 3, c: 4 }
Enter fullscreen mode Exit fullscreen mode

.hasOwnProperty():
This method checks if an object has a specific property and returns a boolean value.

// Example:
const person = {
  name: "John",
  age: 30
};

const hasAge = person.hasOwnProperty("age");
console.log(hasAge); // Output: true
const hasCity = person.hasOwnProperty("city");
console.log(hasCity); // Output: false
Enter fullscreen mode Exit fullscreen mode

Hindi

.keys()
Yeh method ek object ke saare keys (properties) ka ek array return karta hai.

const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const chabiyonKaArray = Object.keys(vyakti);
console.log(chabiyonKaArray); // Output: ["naam", "umar", "shehar"]
Enter fullscreen mode Exit fullscreen mode

.values():
Yeh method ek object ke saare values ka ek array return karta hai.

const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const moolyonKaArray = Object.values(vyakti);
console.log(moolyonKaArray); // Output: ["John", 30, "New York"]
Enter fullscreen mode Exit fullscreen mode

.entries():
Yeh method ek object ke key-value pairs (arrays ke roop mein) ka array return karta hai.

const vyakti = {
  naam: "John",
  umar: 30,
  shehar: "New York"
};

const entryArray = Object.entries(vyakti);
console.log(entryArray);
// Output: [["naam", "John"], ["umar", 30], ["shehar", "New York"]]
Enter fullscreen mode Exit fullscreen mode

.assign();
Yea method ek ya adhik objects ke properties ko ek naye object mein milakar usse combine karta hai.

const pehlaObject = {
  naam: "Shyam",
  umar: 25
};

const doosraObject = {
  shehar: "Mumbai",
  kaam: "Engineer"
};

// `Object.assign()` se do objects ko combine karenge aur ek naya object banayenge.
const combinedObject = Object.assign({}, pehlaObject, doosraObject);

console.log(combinedObject);
// Output: { naam: "Shyam", umar: 25, shehar: "Mumbai", kaam: "Engineer" }
Enter fullscreen mode Exit fullscreen mode

.hasOwnProperty():
Yea method ek object ke andar kisi property ka maujood hone ko check kar sakte hain. Is method ka upayog kisi specific property ke existance ko verify karne ke liye hota hai. Yadi woh property object ke khud ke hai, tabhi yeh method true return karega, anyatha false return karega.

const vyakti = {
  naam: "Shyam",
  umar: 25,
  shehar: "Mumbai"
};

// Check karein ki 'naam' property vyakti object ke andar maujood hai ya nahi
const kyaNaamHai = vyakti.hasOwnProperty('naam');
console.log(kyaNaamHai); // Output: true

// Check karein ki 'address' property vyakti object ke andar maujood hai ya nahi
const kyaAddressHai = vyakti.hasOwnProperty('address');
console.log(kyaAddressHai); // Output: false
Enter fullscreen mode Exit fullscreen mode

Top comments (0)