Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js object filter by keys

const obj = { firstName: 'Jean-Luc', lastName: 'Picard', age: 59 };

// { firstName: 'Jean-Luc', lastName: 'Picard' }
Object.fromEntries(Object.entries(obj).filter(([key]) => key.includes('Name')));
Comment

filter by keyname javascript

Object.fromEntries(Object.entries(obj).filter(([key]) => key.includes('Name')));
Comment

javascript filter array of objects by key

var data = { records : [{ "empid": 1, "fname": "X", "lname": "Y" }, { "empid": 2, "fname": "A", "lname": "Y" }, { "empid": 3, "fname": "B", "lname": "Y" }, { "empid": 4, "fname": "C", "lname": "Y" }, { "empid": 5, "fname": "C", "lname": "Y" }] }
var empIds = [1,4,5]
var filteredArray = data.records.filter(function(itm){
  return empIds.indexOf(itm.empid) > -1;
});

filteredArray = { records : filteredArray };
Comment

filter object by key name

let varName = Object.fromEntries(Object.entries(obj).filter(([key]) => {return key.includes('part_of_keyName') && key.includes('another_part_of_keyName')}));
Comment

javascript filter array of objects by key

const ids = [1, 4, 5],
  data = {
    records: [{
      "empid": 1,
      "fname": "X",
      "lname": "Y"
    }, {
      "empid": 2,
      "fname": "A",
      "lname": "Y"
    }, {
      "empid": 3,
      "fname": "B",
      "lname": "Y"
    }, {
      "empid": 4,
      "fname": "C",
      "lname": "Y"
    }, {
      "empid": 5,
      "fname": "C",
      "lname": "Y"
    }]
  };


data.records = data.records.filter( i => ids.includes( i.empid ) );

console.info( data );
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 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

js object filter by keys

const obj = { firstName: 'Jean-Luc', lastName: 'Picard', age: 59 };

// { firstName: 'Jean-Luc', lastName: 'Picard' }
Object.keys(obj).
  filter((key) => key.includes('Name')).
  reduce((cur, key) => { return Object.assign(cur, { [key]: obj[key] })}, {});
Comment

PREVIOUS NEXT
Code Example
Javascript :: react router route not found redirect 
Javascript :: findindex js 
Javascript :: useref reactjs 
Javascript :: how to filter an array by list of objects in javascript 
Javascript :: adding all elements of an array javascript 
Javascript :: react native vector icon 
Javascript :: get total width of element including padding and border using jquery 
Javascript :: svg component react js 
Javascript :: li dots 
Javascript :: Conditionallu inline styling in react 
Javascript :: javascript hello 
Javascript :: fullcalendar angular add events 
Javascript :: v-btn click 
Javascript :: JavaScript string encryption and decryption 
Javascript :: how to use filter in typescript 
Javascript :: flatten an array javascript 
Javascript :: new Date().toLocaleDateString day 
Javascript :: prototype in javascript 
Javascript :: Divide the number in js 
Javascript :: Use ctrl + scroll to zoom the map & Move map with two fingers on mobile 
Javascript :: Uncaught TypeError: _firebase__WEBPACK_IMPORTED_MODULE_0__.storage.ref is not a function 
Javascript :: how to go back one directory in git bash 
Javascript :: object key as variable 
Javascript :: capitalize first carater js 
Javascript :: type in javascript 
Javascript :: javascript factorial of a number 
Javascript :: how to see if checkbox is checked 
Javascript :: create react without jsx 
Javascript :: groupBy angular 
Javascript :: javascript detect when element is at bottom of screen 
ADD CONTENT
Topic
Content
Source link
Name
3+3 =