Wherefore Art Thou

Tantangan "wherefore art thou" dapat diuraikan lebih jelas dengan metode metode ES 6 Array helper seperti filter and every. things truly do become much clearer. Tantangan ini membuat kita mempelajari bagaimana memanipulasi array dengan es6 array helper dengan mempelajari bagaimana mengaplikasikan reduce, filter, find, map pada sebuah array.

Berikut penjelasan singkat beberapa metode
  • Array.prototype.every()
    • digunakan untuk menguji apakah semua elemen dalam array sesuai dengan kriteria pada fungsi
    • contoh :
    •  function isBelowThreshold(currentValue) {
        return currentValue < 40;
      }
      var array1 = [1, 30, 39, 29, 10, 13];
      console.log(array1.every(isBelowThreshold));
      // expected output: true
  • Array.prototype.filter()
    •  method filter() akan membentuk array baru berisikan elemen elemen yang sesuai dengan kriteria pada fungsi 
    • contoh
    • var words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
      const result = words.filter(word => word.length > 6);
      console.log(result);
      // expected output: Array ["exuberant", "destruction", "present"]
  • Object.prototype.hasOwnProperty()
    • method ini akan memberikan balikan boolean apakah object memiliki propertu yang disebutkan
    • contoh 
    • const object1 = new Object();
      object1.property1 = 42;
      console.log(object1.hasOwnProperty('property1'));
      // expected output: true
      console.log(object1.hasOwnProperty('toString'));
      // expected output: false
      console.log(object1.hasOwnProperty('hasOwnProperty'));
      // expected output: false
  • Object.keys()
    • method yang akan memberikan balikan berupa array yang berisikan nama nama property yang dimiliki oleh object
    • contoh
    • const object1 = {
        a: 'somestring',
        b: 42,
        c: false
      };
      console.log(Object.keys(object1));
      // expected output: Array ["a", "b", "c"] 

Problem
Make a function that looks through an array of objects (first argument) and returns an array of all objects that have matching name and value pairs (second argument). Each name and value pair of the source object has to be present in the object from the collection if it is to be included in the returned array.
For example, if the first argument is [{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], and the second argument is { last: "Capulet" }, then you must return the third object from the array (the first argument), because it contains the name and its value, that was passed on as the second argument.

Langkah langkah penyelesaiannya sebagai berikut :
  • menggunakan Object.keys() method untuk membuat sebuah variabel yang akan berperan sebagai key sumber untuk item yang dicari 
    •  var sourceKey = Object.keys(source); 
  • menggunakan method filter
  •  Untuk setiap kunci dalam setiap item dalam argumen koleksi, periksa apakah kunci argumen sumber cocok
  • memberikan balikan 
Maka solusi dari problem tersebut dapat dituliskan sebagai berikut :
arr = collection.filter(function(obj) {
return sourceKey.every(function(key) {
// Check if the object has the property and the same value.
return obj.hasOwnProperty(key) && obj[key] === source[key];
});
});
whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
 
//Jawaban yang akan dihasilkan { first: “Tybalt”, last: “Capulet” } 
 
 
 
Soal ;
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
// Only change code above this line
return arr;
}

whatIsInAName([{ first: "Romeo", last: "Montague" }, { first: "Mercutio", last: null }, { first: "Tybalt", last: "Capulet" }], { last: "Capulet" });
 
Jawaban
function whatIsInAName(collection, source) {
// What's in a name?
var arr = [];
// Only change code below this line
var sourceKey = Object.keys(source);
arr = collection.filter(function(obj) {
return sourceKey.every(function(key) {
// Check if the object has the property and the same value.
return obj.hasOwnProperty(key) && obj[key] === source[key];
});
});

// Only change code above this line
return arr;
}

Comments