1

I have an array of objects. I want to filter them and then do something to the filtered objects. This is my attempt so far.

Positions Object

    function Position (title, type, purchasable){
    this.title = title;
    this.type = type;
    this.purchasable = purchasable || false;
}
function Purchasable (prices){
    this.owner = "unowned";
    this.rating = 0;
    this.prices = prices;
    this.price = this.prices[this.rating];
}
function City (title,set,prices){
    Position.call(this, title, "city", true);
    Purchasable.call(this, prices);
    this.set = set;
}
positions = [
    new City("Cairo", "brown", [60,2,10,30,90,160,250]),
    new City("Vienna", "brown", [60,4,20,60,180,320,450]),
    new City("Vienna", "blue", [60,4,20,60,180,320,450])
    ];
});

Function

function test() {
    var set = "brown";
    positions.filter(function(obj){
        obj.set === "brown"; //do something to every brown set, eg owner = me
    });
    //I want to change the values of the objs in positions array


}
4
  • is there a question somewhere, hidden really well? Commented Feb 28, 2014 at 17:06
  • can you show your City object? Commented Feb 28, 2014 at 17:10
  • @praveen I edited my question to show the City object. Commented Feb 28, 2014 at 17:19
  • I forgot to mention that I want to change the original obj and not create a new array Commented Feb 28, 2014 at 17:25

3 Answers 3

3
var set = "brown"
var filtered = positions
  .filter(function (obj)
  {
    return obj.set === "brown"
  })
  .map(function (obj)
  {
    obj.owner = me
    return obj
  })
;
console.log(filtered);
Sign up to request clarification or add additional context in comments.

3 Comments

Considering you want only the filtered values, this is correct.... It misses its mark when you want all the remainders from the filter
@emanuelsanga your definition of "filter" is incongruent with the generally accepted one.
anhaa, @justsomebody, semantics pal,, but i believe you got my point.... or is there something I missed there buddy?
0

What about the jQuery .each() function ? https://api.jquery.com/each/

function test() {
    var set = "brown";
    var filtered = positions.filter(function(obj){
        obj.set === "brown"; //do something to every brown set, eg owner = me
    }).each(function(index, item){
         //do something with the object here; your objects are the item parameter

         obj.owner = "me";
    });

}

Comments

0

You need your filter callback to return true/false. You want to return true when the current item should be included in the resulting array and false if it should be excluded. So if you want to filter where obj.set === "brown", do:

var filtered = positions.filter(function(obj){
    if (obj.set === "brown") {
        obj.owner = "me";  // your additional action to perform
        return true;
    }
    return false;
});

As 'just somebody' notes in the comments, It is generally bad practice to make filter do anything other than the filtering part. But this above example shows you how to do this inside of the filter per your original question.

1 Comment

actually, filter is nice because it does (by name) just one thing. you should leave the // do something for a another pass through the filtered array with .map. .filter(function (o) { return o.set == 'brown' }).map(function (o) { mangle(o) })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.