GithubHelp home page GithubHelp logo

Comments (2)

tmcw avatar tmcw commented on June 15, 2024

There currently isn't. I suppose one API that we could expose for this purpose would be something like maxIndex

array = [{id:'1', value: 1}, {id:'2', value: 2}]
index = maxIndex(array.map(item => item.value))
value = array[index].value;
label = array[index].label;

I'm open to this idea, though it does seem like a fairly narrow idea - right now you could get the same effect with:

array = [{id:'1', value: 1}, {id:'2', value: 2}]
maxValue = max(array.map(item => item.value));
maxItem = array.find(item => item.value === maxValue)

This difference only being that the second requires a relatively cheap (maximum O(n)) lookup step.

from simple-statistics.

Yomguithereal avatar Yomguithereal commented on June 15, 2024

The typical solution to this kind of problems is to let users give a function returning the "key" that should serve for comparisons. This is what is achieved by the key kwarg of python's max builtin function and the way _.maxBy is implemented in lodash for instance:

array = [{id:'1', value: 1}, {id:'2', value: 2}]
maxItem = max(array, item => item.value)

But this does not serve exactly the same intent as a maxIndex function as sometimes you really want the index for other reasons. In a perfect world one should have those three functions, but it puts a lot of pressure on the lib's implementation and footprint since it means using sneaky solutions to avoid duplicating too much code all while remaining as performant as possible and if you do so for max, you should probably do so for min and a lot of other functions as well.

from simple-statistics.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.