Home > database >  how to find min value from array of objects over multiple keys in single function
how to find min value from array of objects over multiple keys in single function

Time:08-18

I have to find min value from an array of objects but I have multiple keys and I want to find the min value of all keys or specified keys.

For example

var users = [
    {id:"1", name:"ABC", age:30, height: 75, weight: 83},
    {id:"1", name:"ABC", age:24, height: 63, weight: 75},
    {id:"1", name:"ABC", age:27, height: 56, weight: 55}
]

In this example, I want to find the users with min age, height and weight. But by using a single function.

I am using this function. its finds min age only.

findMin(data) {
        return data.reduce((min, u) => u.age < min ? u.age : min, data[0].age);
    },

CodePudding user response:

You can try this :

const users = [ {id:"1", name:"ABC", age:30, height: 75, weight: 83}, {id:"1", name:"ABC", age:24, height: 63, weight: 75}, {id:"1", name:"ABC", age:27, height: 56, weight: 55} ];
const min = (arr, key) => arr.reduce((min, el) => el[key] < min[key] ? el : min);
const keys = ['age', 'height', 'weight'];
const result = Object.fromEntries(keys.map(k => [k, min(users, k)]));
console.log(result)

Using only 1 function :

const users = [ {id:"1", name:"ABC", age:30, height: 75, weight: 83}, {id:"1", name:"ABC", age:24, height: 63, weight: 75}, {id:"1", name:"ABC", age:27, height: 56, weight: 55} ];

const min = (arr, keys) => arr.reduce(
  (min, el) => Object.fromEntries(keys.map(k => [k, el[k] < min[k][k] ? el : min[k]])), 
  {age: arr[0], height: arr[0], weight: arr[0]}
 );

const result = min(users, ['age', 'height', 'weight']);
console.log(result)

Output :

{
    age: { minAgeItem },
    weight: { minWeightItem },
    height: { minHeightItem }
}

CodePudding user response:

This is my modified version!

const arr = [{
    id: "1",
    name: "ABC",
    age: 30,
    height: 75,
    weight: 83
  },
  {
    id: "1",
    name: "ABC",
    age: 24,
    height: 63,
    weight: 75
  },
  {
    id: "1",
    name: "ABC",
    age: 27,
    height: 56,
    weight: 55
  }
];

function getMins(array) {
  let minAge = Infinity,
    minHeight = Infinity,
    minWeight = Infinity;
  const output = [{}, {}, {}];
  array.forEach((user) => {
    if (user.age <= minAge) {
      minAge = user.age;
      output[0] = user;
    }
    if (user.height <= minHeight) {
      minHeight = user.height;
      output[1] = user;
    }
    if (user.weight <= minWeight) {
      minWeight = user.weight;
      output[2] = user;
    }

  });
  return output;
}

console.log(getMins(arr));

CodePudding user response:

I hope I understand you well, that you want to extract min value for each of fields. Then you could expand your functions to return an object like this:

Note: open chrome console and check output, SO console is giving some weird results!

var users = [
    {id:"1", name:"ABC", age:30, height: 75, weight: 83},
    {id:"1", name:"ABC", age:24, height: 63, weight: 75},
    {id:"1", name:"ABC", age:27, height: 56, weight: 55}
]

function findMin(data) {
  return data.reduce((mins, u) => {
    mins['minAge'] = (u.age < mins['minAge'] ? u.age : mins['minAge']);
    mins['minHeight'] = (u.height < mins['minHeight'] ? u.height : mins['minHeight']);
    mins['minWeight'] = (u.weight < mins['minWeight'] ? u.weight : mins['minWeight']);
    return mins
  }, {
    minAge: data[0].age,
    minHeight: data[0].height,
    minWeight: data[0].weight
  });
}

console.log(findMin(users))

edit: Added more flexibility to the function:

var users = [
{ id: "1", name: "ABC", age: 30, height: 75, weight: 83 },
  { id: "1", name: "ABC", age: 24, height: 63, weight: 75 },
  { id: "1", name: "ABC", age: 27, height: 56, weight: 55 }
];

function findMin(data, keys) {
  return data.reduce((mins, u) => {
    keys.forEach(
      (key) => (mins[key] = u[key] < mins[key] ? u[key] : mins[key])
    );
    return mins;
  }, keys.reduce((accumulator, value) => {
    return {...accumulator, [value]: data[0][value]};
  }, {}));
}

console.log(findMin(users, ['age','height']));

CodePudding user response:

Another one approach

const users = [
    {id:"1", name:"ABC", age:30, height: 75, weight: 83},
    {id:"2", name:"ABC", age:24, height: 63, weight: 75},
    {id:"3", name:"ABC", age:27, height: 56, weight: 55},
];

const getMinsBy = (users, properties) => {
  const minBy = (prop) => users.reduce((min, curr) => (min[prop] > curr[prop]) ? curr : min)
  
  return properties.reduce((acc, prop) => ({ ...acc, [prop]: minBy(prop) }), {});
};

console.log(getMinsBy(users, ['age', 'height', 'weight']));
.as-console-wrapper { max-height: 100% !important; top: 0 }

  • Related