如何从数组中选择某个属性最小的对象?

发布于 2025-01-01 12:27:31 字数 1111 浏览 2 评论 0原文

我刚刚开始了解 Coffescript,并遇到了从数组中选择一个对象的要求,其中特定属性是最小的。我在下面列出了我的基本代码:

class Point
    constructor: (@x, @y, @z) ->

    addedTogether: ->
        @x+@y+@z

class PointCollection
    constructor: ->
        @points = []

    add: (point) ->
        @points.push(point)

    minimalPoint: ->
        // need to return point with lowest addedTogether value

samplePoints = new PointCollection()
samplePoints.add(new Point(1,2,3))
samplePoints.add(new Point(2,3,4))
samplePoints.add(new Point(3,4,5))
samplePoints.add(new Point(4,5,6))
samplePoints.add(new Point(5,6,7))
samplePoints.add(new Point(1,1,1))

lowestValuePoint = samplePoints.minimalPoint()

我显然可以用普通的旧 JavaScript 来实现这一点,其函数如下:

function findPointWithLowestScore(points) {
    var lowestScoringPoint = points[0];

    for (var i = 0; i < points.length; i++) {

    lowestScoringPoint = (points[i].addedTogether() < lowestScoringPoint.addedTogether()) ? points[i] :    lowestScoringPoint;
    }
    return lowestScoringPoint;
}

但是有没有更好、更干净、CoffeeScript-y 的方法来做到这一点?

I'm just getting my head around Coffescript and have come across a requirement to select an object from an array where a particular property is minimal. I've set out my basic code below:

class Point
    constructor: (@x, @y, @z) ->

    addedTogether: ->
        @x+@y+@z

class PointCollection
    constructor: ->
        @points = []

    add: (point) ->
        @points.push(point)

    minimalPoint: ->
        // need to return point with lowest addedTogether value

samplePoints = new PointCollection()
samplePoints.add(new Point(1,2,3))
samplePoints.add(new Point(2,3,4))
samplePoints.add(new Point(3,4,5))
samplePoints.add(new Point(4,5,6))
samplePoints.add(new Point(5,6,7))
samplePoints.add(new Point(1,1,1))

lowestValuePoint = samplePoints.minimalPoint()

I can obviously do that in plain old javascript with a function like:

function findPointWithLowestScore(points) {
    var lowestScoringPoint = points[0];

    for (var i = 0; i < points.length; i++) {

    lowestScoringPoint = (points[i].addedTogether() < lowestScoringPoint.addedTogether()) ? points[i] :    lowestScoringPoint;
    }
    return lowestScoringPoint;
}

but is there a nicer, cleaner, CoffeeScript-y way of doing this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

仅此而已 2025-01-08 12:27:31

尝试这样的事情:

class Point
    constructor: (@x, @y, @z) ->

    addedTogether: ->
        @x+@y+@z

class PointCollection
    constructor: ->
        @points = []

    add: (point) ->
        @points.push(point)

    minimalPoint: ->
        tmp = @points.slice 0 # duplicate array
        tmp.sort (a, b) -> a.addedTogether() - b.addedTogether() # Sort from lowest to highest
        tmp[0] # Return the first element

samplePoints = new PointCollection()
samplePoints.add(new Point(1,2,3))
samplePoints.add(new Point(2,3,4))
samplePoints.add(new Point(3,4,5))
samplePoints.add(new Point(4,5,6))
samplePoints.add(new Point(5,6,7))
samplePoints.add(new Point(1,1,1))

lowestValuePoint = samplePoints.minimalPoint()

Try something like this:

class Point
    constructor: (@x, @y, @z) ->

    addedTogether: ->
        @x+@y+@z

class PointCollection
    constructor: ->
        @points = []

    add: (point) ->
        @points.push(point)

    minimalPoint: ->
        tmp = @points.slice 0 # duplicate array
        tmp.sort (a, b) -> a.addedTogether() - b.addedTogether() # Sort from lowest to highest
        tmp[0] # Return the first element

samplePoints = new PointCollection()
samplePoints.add(new Point(1,2,3))
samplePoints.add(new Point(2,3,4))
samplePoints.add(new Point(3,4,5))
samplePoints.add(new Point(4,5,6))
samplePoints.add(new Point(5,6,7))
samplePoints.add(new Point(1,1,1))

lowestValuePoint = samplePoints.minimalPoint()
星光不落少年眉 2025-01-08 12:27:31

你可以试试这个:

class PointCollection
    # ...
    compare: (p1, p2) -> p1.addedTogether() - p2.addedTogether()

    minimalPoint: ->
        minPoint = points[0]
        minVal = if compare(minPoint, point) > 0 then point else minPoint for point in points

You can try this:

class PointCollection
    # ...
    compare: (p1, p2) -> p1.addedTogether() - p2.addedTogether()

    minimalPoint: ->
        minPoint = points[0]
        minVal = if compare(minPoint, point) > 0 then point else minPoint for point in points
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文