sarpay
12/25/2017 - 7:42 AM

Filter objects in array

Filter objects in array

a = [
  {prop1:"abc", prop2:"qwe"},
  {prop1:"bnmb", prop2:"yutu"},
  {prop1:"zxvz", prop2:"qwrq"}
];

/* jQuery */
var indexes = $.map(a, function(obj, index) {
                if(obj.prop1 == "abc") {
                  return index;
                }
              });
console.log(indexes);/* returns array */

/* ES 6 */
index = a.findIndex(x => x.prop1 == "abc");
console.log(index);

// -- array -- 

myArray = ['a', 'b' , 'c'];
var ix = myArray.findIndex(txt => txt === 'b');
// ix = 1;
const friends = [
    { name: 'Abby', age: 22 },
    { name: 'Boby', age: 16 },
    { name: 'Coel', age: 20 },
    { name: 'Dany', age: 15 },
];

/* ES6  */
var queryResult = friends.filter(x => x.age >= 18);

/* JQuery  */
var queryResult = $.grep(friends, function (e) { return e.age >= 18; });

if (queryResult.length > 0) {
    alert("Bu kişi daha önce eklenmiş.");
    return false;
}