Ondrej Sika

Pop object by key:value of JavaScript Array

06 Nov 2013

Prototype

Require Aray.pop(n)

JavaScript

Array.prototype.popBy = function(key, val){
    for(var i=0; i<this.length; i++){
        if (this[i][key] == val) return this.pop(i);
    }
}

CoffeeScript

Array.prototype.popBy = (key, val) ->
    for i in [0..this.length]
        if this[i][key] == val
            return this.pop i

Example of usage

> array = [{a:0, b: 5}, {a:1, b:6}, {a:2, b:7}, {a:3, b:8}, {a:4, b:9}]
[{a:0, b: 5}, {a:1, b:6}, {a:2, b:7}, {a:3, b:8}, {a:4, b:9}]
> array.popBy("a", 3)
{a:3, b:8}
> array
[{a:0, b: 5}, {a:1, b:6}, {a:2, b:7}, {a:4, b:9}]
> array.popBy("b", 6)
{a:1, b:6}
> array
[{a:0, b: 5}, {a:2, b:7}, {a:4, b:9}]

Share on Facebook, Twitter, Google+, Linkedin

comments powered by Disqus

--