
- Javascript Basics Tutorial
- Javascript - Home
- Javascript - Overview
- Javascript - Syntax
- Javascript - Enabling
- Javascript - Placement
- Javascript - Variables
- Javascript - Operators
- Javascript - If...Else
- Javascript - Switch Case
- Javascript - While Loop
- Javascript - For Loop
- Javascript - For...in
- Javascript - Loop Control
- Javascript - Functions
- Javascript - Events
- Javascript - Cookies
- Javascript - Page Redirect
- Javascript - Dialog Boxes
- Javascript - Void Keyword
- Javascript - Page Printing
- JavaScript Objects
- Javascript - Objects
- Javascript - Number
- Javascript - Boolean
- Javascript - Strings
- Javascript - Arrays
- Javascript - Date
- Javascript - Math
- Javascript - RegExp
- Javascript - HTML DOM
- JavaScript Advanced
- Javascript - Error Handling
- Javascript - Validations
- Javascript - Animation
- Javascript - Multimedia
- Javascript - Debugging
- Javascript - Image Map
- Javascript - Browsers
- JavaScript Useful Resources
- Javascript - Questions And Answers
- Javascript - Quick Guide
- Javascript - Functions
- Javascript - Resources
Filtering of JavaScript object
Here we need to create a function that takes in an object and a search string and filters the object keys that start with the search string and returns the object
Here is the code for doing so −
Example
const obj = { "PHY": "Physics", "MAT": "Mathematics", "BIO": "Biology", "COM": "Computer Science", "SST": "Social Studies", "SAN": "Sanskrit", "ENG": "English", "HIN": "Hindi", "ESP": "Spanish", "BST": "Business Studies", "ECO": "Economics", "CHE": "Chemistry", "HIS": "History" } const str = 'en'; const returnFilteredObject = (obj, str) => { const filteredObj = {}; Object.keys(obj).forEach(key => { if(key.substr(0, str.length).toLowerCase() === str.toLowerCase()){ filteredObj[key] = obj[key]; } }); return filteredObj; }; console.log(returnFilteredObject(obj, str));
Code explanation −
We simply iterate over each key of the object, if it starts with the str we received as argument, then we save it in another object otherwise we keep on iterating.
For the purpose of this very problem, we iterated through each key and shoved the required key in a new object, but for a more performant solution instead of creating a new object we could have just deleted the unwanted properties from the original object.
Output
Output in the console will be −
{ ENG:"English" }
- Related Articles
- Filtering array of objects in JavaScript
- Filtering array within a limit JavaScript
- JavaScript - filtering array with an array
- Filtering out only null values in JavaScript
- Filtering out primes from an array - JavaScript
- Filtering out numerals from string in JavaScript
- Filtering array to contain palindrome elements in JavaScript
- Array filtering using first string letter in JavaScript
- Filtering string to contain unique characters in JavaScript
- jQuery Traversing Filtering
- ArduinoJSON: Filtering Data
- Filtering out the non-unique value to appear only once in JavaScript
- Length of a JavaScript object?
- JavaScript: Sort Object of Objects
- Bilateral filtering using OpenCV

Advertisements