Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript (jquery) functionality similar to linq's Where()

Is it possible to do in jquery something similar to this C# example:

LoopModel = Model.Fields
                    .Where(p => p.Key < 1000 && !Model.FieldHandled.ContainsKey(p.Key) && !FieldsValid.ContainsKey(p.Key))
                    .OrderBy(p => p.Value.SortOrder).ThenBy(p => p.Value.FieldTypeID).ThenBy(p => p.Value.FieldLabel);

I can do

var fields = @Html.Raw(JsonConvert.SerializeObject(Model.Fields));

So I want to be able to select certain fields from "fields".

Thanks

like image 389
Dmitriy Reznik Avatar asked Feb 25 '26 08:02

Dmitriy Reznik


2 Answers

Use filter. No libraries required.

Here is an example from the link:

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.length > 6);

console.log(result);
// expected output: Array ["exuberant", "destruction", "present"]
like image 110
James Brierley Avatar answered Feb 26 '26 20:02

James Brierley


I prefer Lodash or Underscore. They are widely used in various JS libs and right now I think they are the most JavaScript-ish solution. They also guarantee support for legacy browsers, having lots of performance enhancements. I think it's worth to learn them.

https://lodash.com/ http://underscorejs.org/

See the comparison here: Differences between lodash and underscore

like image 41
Gábor Imre Avatar answered Feb 26 '26 21:02

Gábor Imre



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!