Code reduce/reusability technique in javascript/typescript
I have array object as follows.
var myArray = [
{ id: 20, id1: 'Captain Piett', idn: 2000 },
{ id: 24, id1: null, idn: 5000 },
{ id: 56, id1: 'Admiral Ozzel', idn: 2500 },
{ id: 88, id1: 'Commander Jerjerrod', idn: 1000 }
];
From above, I want to perform below operations for every property.
- For number col, get min, max values
- For string col, get minLength, maxLength
I could write as below
For number property
const m = Math.min(...(this.myArray.map(el => el.id)));
const m = Math.max(...(this.myArray.map(el => el.id)));
For string property
const m = Math.min(...(this.myArray.map(el => el.id1 ? el.id1.length : 0)));
const m = Math.max(...(this.myArray.map(el => el.id1 ? el.id1.length : 0)));
I have almost 50 properties in myArray. Is there any code reusability technique to achieve this, instead of writing 50 * 2 statements?
col...?for(const key in arr[0]) if(<check value type>)Math.max(...array.map(o => o[key]). But the problem is if any of the properties are null, then it will be hard to know if it is astringor anumber.