windows 10 pro office 2019 pro office 365 pro windows 10 home windows 10 enterprise office 2019 home and business office 2016 pro windows 10 education visio 2019 microsoft project 2019 microsoft project 2016 visio professional 2016 windows server 2012 windows server 2016 windows server 2019 Betriebssysteme office software windows server https://softhier.com/ instagram takipçi instagram beğeni instagram görüntüleme instagram otomatik beğeni facebook beğeni facebook sayfa beğenisi facebook takipçi twitter takipçi twitter beğeni twitter retweet youtube izlenme youtube abone instagram

javascript – get max/min of multiple subarrays

Asked By: Anonymous

Say below data structure:

var data = [
  {
    name: "USA",
    values: [
      {date: "2008", price: "20"},
      {date: "2009", price: "30"}
    ]
  },
  {
    name: "Canada",
    values: [
      {date: "2000", price: "40"},
      {date: "2001", price: "50"}
    ]
  },
  {
    name: "Maxico",
    values: [
      {date: "2006", price: "60"},
      {date: "2007", price: "70"}
    ]
  }
];

I would like to get the min/max of all price field in multiple subarrays, the arrays number currently is 3 but it maybe changed.

The function minmax(data) should output ["20","70"] for this sample!


Solution

Answered By: Anonymous

Write a prices traversal and a generic minmax method

_x000D_

_x000D_

function* prices(t) {
  switch (t?.constructor) {
    case Array:
      for (const v of t)
        yield *prices(v)
      break
    case Object:
      if (t?.values)
        yield *prices(t.values)
      else
        yield t?.price
      break
  }
}

function minmax(t) {
  let min = Infinity
  let max = -Infinity
  for (const v of t) {
    min = Math.min(min, v)
    max = Math.max(max, v)
  }
  return {min, max}
}

const data =
  [{name: "USA",values: [{date: "2008", price: "20"},{date: "2009", price: "30"}]},{name: "Canada",values: [{date: "2000", price: "40"},{date: "2001", price: "50"}]},{name: "Maxico",values: [{date: "2006", price: "60"},{date: "2007", price: "70"}]}];
  
console.log(minmax(prices(data)))
// { min: 20, max: 70 }

console.log(minmax([]))
// { min: Infinity, max: -Infinity }

_x000D_

_x000D_

x000D

techinplanet staff

Porno Gratuit Porno Français Adulte XXX Brazzers Porn College Girls Film érotique Hard Porn Inceste Famille Porno Japonais Asiatique Jeunes Filles Porno Latin Brown Femmes Porn Mobile Porn Russe Porn Stars Porno Arabe Turc Porno caché Porno de qualité HD Porno Gratuit Porno Mature de Milf Porno Noir Regarder Porn Relations Lesbiennes Secrétaire de Bureau Porn Sexe en Groupe Sexe Gay Sexe Oral Vidéo Amateur Vidéo Anal

Windows 10 Kaufen Windows 10 Pro Office 2019 Kaufen Office 365 Lizenz Windows 10 Home Lizenz Office 2019 Home Business Kaufen windows office 365 satın al follower kaufen instagram follower kaufen porno