“ITerate Over Array Objects JavaScript” Kode Jawaban

Loop JavaScript melalui array objek

var person={
 	first_name:"johnny",
  	last_name: "johnson",
	phone:"703-3424-1111"
};
for (var property in person) {
  	console.log(property,":",person[property]);
}
Grepper

Loop JavaScript melalui array objek

let arr = [object0, object1, object2];

for (let elm of arr) {
  console.log(elm);
}
garzj

ITerate Over Array Objects JavaScript

// Find an element in an array

const people = [ {name: "john", age:23},
                {name: "john", age:43},
                {name: "jim", age:101},
                {name: "bob", age:67} ];

const john = people.find(person => person.name === 'john');
console.log(john);
Shan Thiru

ITerate Over Array Objects JavaScript

// Sort an array

const people = [
  { name: "John", age: 21 },
  { name: "Peter", age: 31 },
  { name: "Andrew", age: 29 },
  { name: "Thomas", age: 25 }
];

let sortByAge = people.sort(function (p1, p2) {
  return p1.age - p2.age;
});

console.log(sortByAge);
Shan Thiru

ITerate Over Array Objects JavaScript

// Sum up a particular property, and calculate its average

const myArray = [{x:100}, {x:200}, {x:300}];

const sum = myArray.map(element => element.x).reduce((a, b) => a + b, 0);
console.log(sum); // 600 = 0 + 100 + 200 + 300

const average = sum / myArray.length;
console.log(average); // 200
Shan Thiru

ITerate Over Array Objects JavaScript

// Create a new array based on the original but without modifying it

const myArray = [{x:100}, {x:200}, {x:300}];

const newArray= myArray.map(element => {
    return {
        ...element,
        x: element.x * 2
    };
});

console.log(myArray); // [100, 200, 300]
console.log(newArray); // [200, 400, 600]
Shan Thiru

Jawaban yang mirip dengan “ITerate Over Array Objects JavaScript”

Pertanyaan yang mirip dengan “ITerate Over Array Objects JavaScript”

Lebih banyak jawaban terkait untuk “ITerate Over Array Objects JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya