Ramesh
Ramesh

Reputation: 1145

Code reduce/reusability technique in javascript/typescript

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.

  1. For number col, get min, max values
  2. 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?

Upvotes: 0

Views: 115

Answers (3)

User863
User863

Reputation: 20039

Instead of traversing multiple time using map(), you can get all minimum and maximum values once as an object like this

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 }
];

const getExtremes = (arr) => {
  return arr.reduce((a, v) => {
    for (let k in v) {
      let len = 0
      if (typeof v[k] === 'string') len = v[k].length
      else if (typeof v[k] === 'number') len = v[k]
      a['max'][k] = Math.max(len, a['max'][k] === undefined ? -Infinity : a['max'][k])
      a['min'][k] = Math.min(len, a['min'][k] === undefined ? Infinity : a['min'][k])
    }
    return a
  }, { min: {}, max: {} })
}

console.log(getExtremes(myArray))

Upvotes: 1

Nina Scholz
Nina Scholz

Reputation: 386560

You could take some functions and get the array with the wanted key and types and take later the min and max values.

const
    getKey = k => o => o[k],
    getLength = k => o => o[k] ? o[k].length : 0; 
    map = fn => array => array.map(fn);

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 }],
    ids = map(getKey('id'))(myArray),
    stringLengths = map(getLength('id1'))(myArray);

console.log(Math.min(...ids));
console.log(Math.max(...ids));

console.log(Math.min(...stringLengths));
console.log(Math.max(...stringLengths));

Upvotes: 0

Matt
Matt

Reputation: 451

Just come up with some config object. it would look something like this

const config = {
  propertyName: string,
  propertyType: string,
}

Then have a function that takes in your data and an array of these config objects... one config for each property in the object. Then have a compare function for each type of propertyType. when you pass ur data and the config in choose what kind of compare function to use with a switch statement depending on the property type. Then plug that into a reduce function and youll get what you want. There is a lot of missing details because this question is not meant for this site and its pretty involved but thats kinda how you would do it.

Upvotes: 0

Related Questions