Leran JavaScripts

JavaScript Object Methods
General Methods
JavaScript Object.assign()

The Object.assign() method copies properties from one or more source objects to a target object. Example::

// Create Target Object
const person1 = {
  firstName: "John",
  lastName: "Doe",
  age: 28
};

// Create Source Object
const person2 = {firstName: "Anne",lastName: "Smith"};

// Assign Source to Target
Object.assign(person1, person2)
              
JavaScript Object.entries()
Object.entries() returns an array of the key/value pairs in an object:
Example::

const person = {
  firstName : "John",
  lastName : "Doe",
  age : 28
};

let text = Object.entries(person);
              
use objects in loops and maps:

const fruits = {Bananas:300, Oranges:200, Apples:500};
let text = "";
for (let [fruit, value] of Object.entries(fruits)) {
  text += fruit + ": " + value + "
"; }
map:

const fruits = {Bananas:300, Oranges:200, Apples:500};

const myMap = new Map(Object.entries(fruits));
              
JavaScript Object.fromEntries()
This method creates an object from a list of key/value pairs::

const fruits = [
  ["apples", 300],
  ["pears", 900],
  ["bananas", 500]
];

const myObj = Object.fromEntries(fruits);
              
JavaScript Object.values()
Object.values() returns a single dimension array of the object values:

const person = {
  firstName : "John",
  lastName : "Doe",
  age : 28
};

let text = Object.values(person);
              
JavaScript Object.groupBy()
The Object.groupBy() method groups elements of an object according to string values returned from a callback function.
The Object.groupBy() method does not change the original object.

// Create an Array
const fruits = [
  {name:"apples", quantity:300},
  {name:"bananas", quantity:500},
  {name:"oranges", quantity:200},
  {name:"kiwi", quantity:150}
];
// Callback function to Group Elements
function myCallback({ quantity }) {
  return quantity > 200 ? "ok" : "low";
}
// Group by Quantity
const result = Object.groupBy(fruits, myCallback);
                
JavaScript Object.keys()
This method returns an array with the keys of an object.

// Create an Object
const person = {
  firstName: "John",
  lastName: "Doe",
  age: 28
};
// Get the Keys
const keys = Object.keys(person);
              
JavaScript for...in Loop
for...in statement loops through the properties of an object.
Syntax
Looping through the properties of an object:

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

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