“Properti Filter dari Object JavaScript” Kode Jawaban

dapat memfilter digunakan pada objek dalam javascript

var heroes = [
	{name: “Batman”, franchise: “DC”},
	{name: “Ironman”, franchise: “Marvel”},
	{name: “Thor”, franchise: “Marvel”},
	{name: “Superman”, franchise: “DC”}
];

var marvelHeroes =  heroes.filter(function(hero) {
	return hero.franchise == “Marvel”;
});

// [ {name: “Ironman”, franchise: “Marvel”}, {name: “Thor”, franchise: “Marvel”} ]
Bright Beaver

Properti Filter dari Object JavaScript

const person = {
  first_name: 'John',
  last_name: 'Smith',
  id: '0000-0000-0000-0001',
  email: 'js-example.example.com',
  taxID: '000-111-5555'
}

const displayOnly = ['first_name', 'last_name', 'email']

//Object.fromEntries converts Object.entries back into an object
const results  = Object.fromEntries(
  //Object.entries creates an array of the key value pairs in the object.
  Object.entries(person).filter(entry => {
    const property_name = entry[0];
    
    //filter out keys based on the array
    if (displayOnly.includes(property_name)) {
      return entry
    }
  })
);
  
console.log(results)
JustALittleHeat

Jawaban yang mirip dengan “Properti Filter dari Object JavaScript”

Pertanyaan yang mirip dengan “Properti Filter dari Object JavaScript”

Lebih banyak jawaban terkait untuk “Properti Filter dari Object JavaScript” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya