js filter an object code example

Example 1: how to filter object in javascript

// use filter() function. Let say we have array of object.
let arr = [
  {name: "John", age: 30},
  {name: "Grin", age: 10},
  {name: "Marie", age: 50},
];
//Now I want an array in which person having age more than 40 is not
//there, i.e, I want to filter it out using age property. So
let newArr = arr.filter((person)=>(return person.age <= 40));
//filter function expects a function if it return true then it added 
//into new array, otherwise it is ignored. So new Array would be
/* [
	{name: "John", age: 30},
  	{name: "Grin", age: 10},
   ]
*/

Example 2: who to accses to an object vallue inside an array with .filter method js

const array = [
  {
    username: "john",
    team: "red",
    score: 5,
    items: ["ball", "book", "pen"]
  },
  {
    username: "becky",
    team: "blue",
    score: 10,
    items: ["tape", "backpack", "pen"]
  },
  {
    username: "susy",
    team: "red",
    score: 55,
    items: ["ball", "eraser", "pen"]
  },
  {
    username: "tyson",
    team: "green",
    score: 1,
    items: ["book", "pen"]
  },

];
//FILTER MEMBERS IN TEAM "RED"
const filterArray=array.filter(word=>word.team==="red")

Example 3: javascript object filter

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);

Example 4: js filter object

/** filter object by key or value */

/** filter object function */
function filterObj( obj, predicate ) {
	var result = {}, key;

	for ( key in obj ) {
		if ( obj.hasOwnProperty( key ) && predicate( key, obj[ key ] ) ) {
			result[ key ] = obj[ key ];
		}
	}

	return result;
};

// example

// set object
var obj = {
	name : 'john',
	lastName : 'smith',
	age : 32
}

// filter out parameters using key and value
var filteredObj = filterObj( obj, function( key, value ) {
	return key !== 'age' && value !== 'smith'
});

// show result
console.log( filteredObj ); // { name: "john" }

Example 5: filter out arrays js

let newArray = array.filter(function(item) {
  return condition;
});

Example 6: js filter object of objects

// Our data
const fruits = {
  apple: {
    qty: 300,
    color: "green",
    name: "apple",
    price: 2
  },
  banana: {
    qty: 130,
    color: "yellow",
    name: "banana",
    price: 3
  },
  orange: {
    qty: 120,
    color: "orange",
    name: "orange",
    price: 1.5
  },
  melon: {
    qty: 70,
    color: "yellow",
    name: "melon",
    price: 5
  }
};
// Now let"s create a map function
const map = (obj, fun) =>
  Object.entries(obj).reduce(
    (prev, [key, value]) => ({
      ...prev,
      [key]: fun(key, value)
    }),
    {}
  );
// Finally let's map by color for example
const myFruits = map(fruits, (_, fruit) => fruit.color);
/*
{ apple: 'green',
  banana: 'yellow',
  orange: 'orange',
  melon: 'yellow' }
/*