“Filter Objek JavaScript ES6” Kode Jawaban

Filter Objek JavaScript

function objectFilter = (obj, predicate) => 
    Object.keys(obj)
          .filter( key => predicate(obj[key]) )
          .reduce( (res, key) => (res[key] = obj[key], res), {} );

// Example use:
var scores = {
    John: 2, Sarah: 3, Janet: 1
};

var filtered = objectFilter(scores, num => num > 1); 
console.log(filtered);
QuietHumility

Filter Objek JavaScript ES6

let arr = [
  {name: "John", age: 30},
  {name: "Grin", age: 10},
  {name: "Marie", age: 50},
];

//An array showing persons with age less than 40 
//is given by the following syntax

let newArr = arr.filter(name=>name.age < 40));

console.log(newArray);   // => [{name: "John", age: 30}, {name: "Grin", age: 10},] 
TechLover

Jawaban yang mirip dengan “Filter Objek JavaScript ES6”

Pertanyaan yang mirip dengan “Filter Objek JavaScript ES6”

Lebih banyak jawaban terkait untuk “Filter Objek JavaScript ES6” di JavaScript

Jelajahi jawaban kode populer menurut bahasa

Jelajahi bahasa kode lainnya