Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVASCRIPT

filter properties from object javascript

const person = {
  first_name: 'John',
  last_name: 'Smith',
  id: '0000-0000-0000-0001',
  email: 'js-example.example.com',
  taxID: '000-111-5555'
}

const displayOnly = ['first_name', 'last_name', 'email']

//Object.fromEntries converts Object.entries back into an object
const results  = Object.fromEntries(
  //Object.entries creates an array of the key value pairs in the object.
  Object.entries(person).filter(entry => {
    const property_name = entry[0];
    
    //filter out keys based on the array
    if (displayOnly.includes(property_name)) {
      return entry
    }
  })
);
  
console.log(results)
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #filter #properties #object #javascript
ADD COMMENT
Topic
Name
2+9 =