0

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?

5
  • what is col...? Commented Jul 17, 2019 at 9:10
  • Sorry, read it as property Commented Jul 17, 2019 at 9:10
  • 2
    If the code works and objective of this post is optimization/ improvements, CodeReviews is the right forum for it Commented Jul 17, 2019 at 9:11
  • You could loop through the keys of first object and get the min and max for the array for each property 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 a string or a number. Commented Jul 17, 2019 at 9:13
  • @NickParsons, I want to perform these operations for every property. I given example, if property is number then some action else string, some action. But it should repeat for every property in myArray. Commented Jul 17, 2019 at 9:14

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

id1 has null property. It should return 0 as minLength.
0

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.

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.