最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

arrays - Returning a tuple in Javascript - Stack Overflow

programmeradmin0浏览0评论

I have an array of pokemon objects which have a Max and Min height (see snapshot below) and I need to return the average minimum and maximum height for all pokemon in the form of a tuple

const allPokemon = [
  {
    Height: {
      Minimum: '0.61m',
      Maximum: '0.79m',
    },
  },
];

I need to use HOFs so I was thinking to do something like this for both Maximum and Minimum height:

allPokemon.reduce((acc, cur) => acc + parseInt(cur.Height.Maximum), 0) / allPokemon.length;
allPokemon.reduce((acc, cur) => acc + parseInt(cur.Height.Minimum), 0) / allPokemon.length;

but I'm not sure how to return the two values in the form of a tuple (create empty array and push the values in? Concat method?)

Any help would be much appreciated.

I have an array of pokemon objects which have a Max and Min height (see snapshot below) and I need to return the average minimum and maximum height for all pokemon in the form of a tuple

const allPokemon = [
  {
    Height: {
      Minimum: '0.61m',
      Maximum: '0.79m',
    },
  },
];

I need to use HOFs so I was thinking to do something like this for both Maximum and Minimum height:

allPokemon.reduce((acc, cur) => acc + parseInt(cur.Height.Maximum), 0) / allPokemon.length;
allPokemon.reduce((acc, cur) => acc + parseInt(cur.Height.Minimum), 0) / allPokemon.length;

but I'm not sure how to return the two values in the form of a tuple (create empty array and push the values in? Concat method?)

Any help would be much appreciated.

Share Improve this question edited Jan 22, 2021 at 10:09 Ali Esmailpor 1,2013 gold badges12 silver badges23 bronze badges asked Jan 22, 2021 at 9:50 ruggleruggle 1191 silver badge7 bronze badges 1
  • object would be helpful. – Nguyễn Văn Phong Commented Jan 22, 2021 at 9:52
Add a ment  | 

4 Answers 4

Reset to default 3

For now, JavaScript doesn’t have tuples. Besides, Javascript is weakly typed

Weakly typed means the piler, if applicable, doesn't enforce correct typing. Without implicit piler interjection, the instruction will error during run-time.

However, with the object or array destructuring you can archive it

  1. You can use .reduce with initial data as object like { Maximum: 0, Minimum: 0 } to aggregate your data like below:

const allPokemon = [
        {
          Height: {
            Minimum: "1.61m",
            Maximum: "2.79m"
           }
        }]
        
var result = allPokemon.reduce((acc, cur) => {
      acc.Maximum += (parseFloat(cur.Height.Maximum)/allPokemon.length);      
      acc.Minimum += (parseFloat(cur.Height.Minimum)/allPokemon.length);
      
      return acc; 
}, { Maximum: 0, Minimum: 0 });

console.log(result);

  1. The second way is using array then destructuring assignment like this

const allPokemon = [
        {
          Height: {
            Minimum: "1.61m",
            Maximum: "2.79m"
           }
        }]
        
var result = allPokemon.reduce((acc, cur) => {
      acc[0] += (parseFloat(cur.Height.Maximum)/allPokemon.length);      
      acc[1] += (parseFloat(cur.Height.Minimum)/allPokemon.length);
      
      return acc; 
}, [0, 0]);

console.log(result);

// Array Destructuring here
const [Maximum, Minimum] = result;
console.log("Maximum: " + Maximum);
console.log("Minimum: " + Minimum);

For example:

const [a, b] = [10, 20]; // a = 10, b= 20

Note: As @VLAZ 's ment, in the near future we'd have proper tuples and records like this one proposal-record-tuple

const allPokemon = [
        {
          Height: {
            Minimum: "1.61m",
            Maximum: "2.79m"
           }
        }]
        
const [avgMin, avgMax] = allPokemon.reduce((acc, cur) => {
      acc[0] += parseFloat(cur.Height.Minimum);      
      acc[1] += parseFloat(cur.Height.Maximum);
      
      return acc; 
}, [0, 0]).map(total => total / allPokemon.length)

console.log(`Average min: ${avgMin}\nAverage max: ${avgMax}`);

The first value of the result tuple is the average minimum and the second is the average maximum.

There's no tuple in JavaScript, you may return an array instead. JS will unpack into variables.

function foo(){
    return ["value1","value2"];
}

[var1,var2] = foo();
console.log(var1,var2);

I don't think you need hof (look at others answers) however if you really want to use hof (e.g as an exercise) you need a function which takes a function as parameter

So e.g

take a function which extracts the numerical property and take a function which uses it to average the data.

function getValue(pokemon) {
  return pokemon.height.Maximum
}

function avgBy(getValue, pokemons) {
  return pokemons.length === 0 ? 0 : pokemons.reduce((s, p) => s + getValue(p), 0) / pokemons.length
}

Then you can indeed pose:

function getMin (pokemon) { return pokemon.height.Minimum }
function getMax (pokemon) { return pokemon.height.Maximum }
const [min, max] = [avgBy(getMin, pokemons), avgBy(getMax, pokemons)]

The nice property is now you have the avgBy which can pute the avg of whatever as long you give it a proper getValue func

function getMin (pokemon) { return pokemon.height.Minimum }
function getMax (pokemon) { return pokemon.height.Maximum }

function avgBy(getValue, pokemons) {
  return pokemons.length === 0 ? 0 : pokemons.reduce((s, p) => s + getValue(p), 0) / pokemons.length
}
const pokemons = [{height:{Maximum: 1, Minimum: 0}}, {height:{Maximum: 3, Minimum:1}}]
const [min, max] = [avgBy(getMin, pokemons), avgBy(getMax, pokemons)]
console.log({min, max})

发布评论

评论列表(0)

  1. 暂无评论