Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Filter object properties by key in ES6

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = Object.keys(raw)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    obj[key] = raw[key];
    return obj;
  }, {});

console.log(filtered);
 Run code snippet
Comment

Filter object properties by key in ES6

const raw = {
  item1: { key: 'sdfd', value:'sdfd' },
  item2: { key: 'sdfd', value:'sdfd' },
  item3: { key: 'sdfd', value:'sdfd' }
};

const allowed = ['item1', 'item3'];

const filtered = Object.keys(raw)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    obj[key] = raw[key];
    return obj;
  }, {});

console.log(filtered);
 Run code snippet
Comment

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

PREVIOUS NEXT
Code Example
Javascript :: js random seed 
Javascript :: spread operator es6 
Javascript :: what are the comparison operators in javascript 
Javascript :: firebase.database.ServerValue.increment 
Javascript :: reach last array js 
Javascript :: angular architecture patterns 
Javascript :: javascript function syntax 
Javascript :: why does array index start from 0 
Javascript :: how to check if a user is logged in javascript 
Javascript :: js arrow function vs function 
Javascript :: how to remove an element from an array javascript 
Javascript :: determine location of ip address nodejs 
Javascript :: hide react source 
Javascript :: javscript async function 
Javascript :: simple website with html css and javascript 
Javascript :: js parse bool 
Javascript :: js role giver 
Javascript :: javascript find ip and information 
Javascript :: how do you calculate what percentage a number is of another number 
Javascript :: chart js clear out chart 
Javascript :: js remove several elements from array 
Javascript :: express delete session variable 
Javascript :: loading button jquery 
Javascript :: sum all odd in binary search tree recursion javascript 
Javascript :: is js dead 
Javascript :: Material-ui alarm icon 
Javascript :: timeout for javascript 
Python :: jupyter display all columns 
Python :: matplotlib dark mode 
Python :: check python 32 or 64 
ADD CONTENT
Topic
Content
Source link
Name
3+7 =