js function pick properties from object code example

Example 1: js function pick properties from object

function pick (obj: { [key: string]: any }, props: string[] | string) {
	const propsArray = Array.isArray(props) ? props : props.split(' ')
	var picked: {
    	[key: string]: any
    } = {};
  
	propsArray.forEach(function(prop) {
		picked[prop] = obj[prop];
	});
  
	return picked
};

Example 2: js select keys from object

const object = { a: 5, b: 6, c: 7  };
const picked = (({ a, c }) => ({ a, c }))(object);

console.log(picked); // { a: 5, c: 7 }

Example 3: javascript object get subset

const object = { a: 5, b: 6, c: 7  };const subset = (({ a, c }) => ({ a, c }))(object);console.log(subset); // { a: 5, c: 7 }