PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 24, 2022

[FIXED] How to find the minimum, maximum and average value of all property of array of objects

 July 24, 2022     arrays, javascript, json, node.js, reactjs     No comments   

Issue

I have an array of objects with different values in it, I want to find the min, max, and average of the properties in that array

for eg if I have an array

const array = [{
    "a": "-0.06",
    "b": "0.25",
    "c": "-0.96",
    "d": "-0.14"
  },
  {
    "a": "-0.37",
    "b": "0.01",
    "c": "-0.77",
    "d": "-0.09"
  },
  {
    "a": "0.01",
    "b": "0.88",
    "c": "-0.53",
    "d": "-0.28"
  },
  {
    "a": "0.53",
    "b": "-0.62",
    "c": "0.02",
    "d": "0.74"
  },
  {
    "a": "0.79",
    "b": "-0.39",
    "c": "0.70",
    "d": "0.18"
  },
  {
    "a": "0.74",
    "b": "-0.14",
    "c": "0.22",
    "d": "-0.58"
  }
]

So the output will be as given below

const out = [{
  property: a,
  minValue: -0.37,
  maxValue: .79,
  avg: 0.2733333333333334
}, {
  property: b,
  minValue: -.62,
  maxValue: .88,
  avg: -0.0016666666666666496
}, {
  property: c,
  minValue: -.96,
  maxValue: .07,
  avg: -0.21999999999999997
}, {
  property: d,
  minValue: -.58,
  maxValue: .74,
  avg: -0.028333333333333332
}]

So to get the output we iterate to the array and find the minimum, maximum, and average value of a, b , c and d and store it in a new array


Solution

I have followed a two step process -

  1. Group the values of each property (using reduce for this)
  2. Loop through all the properties and compute the required result.

const array = [{    "a": "-0.06",    "b": "0.25",    "c": "-0.96",    "d": "-0.14"  },  {    "a": "-0.37",    "b": "0.01",    "c": "-0.77",    "d": "-0.09"  },{    "a": "0.01",    "b": "0.88",    "c": "-0.53",    "d": "-0.28"  },  {    "a": "0.53",    "b": "-0.62",    "c": "0.02",    "d": "0.74"  },  {    "a": "0.79",    "b": "-0.39",    "c": "0.70",    "d": "0.18"  },  {    "a": "0.74",    "b": "-0.14",    "c": "0.22",    "d": "-0.58"  }]

const out = [];

const newObj = array.reduce((prevValue, currValue) => {
  Object.keys(currValue).forEach((el) => {
    if (prevValue[el]) {
      prevValue[el].push(Number(currValue[el]));
    } else {
      prevValue[el] = [Number(currValue[el])];
    }
  });
  return prevValue;
}, {});

Object.keys(newObj).forEach((el) => {
  out.push({
    property: el,
    minValue: Math.min(...newObj[el]),
    maxValue: Math.max(...newObj[el]),
    avg: newObj[el].reduce((a, b) => a + b, 0) / newObj[el].length
  });
});

console.log(out);



Answered By - jateen
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing