0

i'm trying to remove value from existing array. i have no idea to how to check the array for specific value and remove it. my array looks like

["Criteria.Make=scion", "Criteria.ModelName=xb", "Criteria.PriceMax=4000", "Criteria.ZipCode=23212", "Criteria.MaxDistance=50"]

i want to check if array exist Criteria.PriceMax={what ever dynamically generated value} i need to remove from the array.

i know i can use grep to remove it but i can come up with the exact need with my code

I check that value like this

$.inArray("Criteria.PriceMax",searchUrlBuilder)) > -1

won't work properly regex match will be the best but i have no idea how to do that.

thanks

2
  • Can you change your array to an object? Something like { Criteria: [ Model: 'xb', PriceMax: 4000 ]}. This way you wont need any regex hacking to find the property you need. Commented Jul 17, 2014 at 6:37
  • Rory that won't be possible on my scenario . Commented Jul 17, 2014 at 6:55

2 Answers 2

1

My first thought would be to use Array.prototype.filter():

var filteredArray = ["Criteria.Make=scion", "Criteria.ModelName=xb", "Criteria.PriceMax=4000", "Criteria.ZipCode=23212", "Criteria.MaxDistance=50"].filter(function(a){
    return a.indexOf('Criteria.PriceMax') < 0;
});

// returns: ["Criteria.Make=scion", "Criteria.ModelName=xb", "Criteria.ZipCode=23212", "Criteria.MaxDistance=50"] 

JS Fiddle demo.

References:

Sign up to request clarification or add additional context in comments.

4 Comments

I think we posted our answers at exactly the same second!
Array.filter is just that good! :) I think that's the first time that's happened to me so far, down to the actual second...
Me too. :) First time posting at the exact second! In any case, yours is imo better since this trivial requirement does not really need a regex engine match.
i think this is more acceptable rather go for regex check. thanks David
1

You can use Array.filter():

var arr = ["Criteria.Make=scion", "Criteria.ModelName=xb", "Criteria.PriceMax=4000", "Criteria.ZipCode=23212", "Criteria.MaxDistance=50"];

arr = arr.filter(function(_item) {
    return !_item.match(/^Criteria\.PriceMax/);
});

returns

["Criteria.Make=scion", "Criteria.ModelName=xb", "Criteria.ZipCode=23212", "Criteria.MaxDistance=50"]

Comments

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.