underscore .find() multiples with same key value - javascript

I am currently trying to filter an object via POST from a form the form is made of checkboxes and as a result I need to be able to search an array of objects for multiple values on the same key.
My POST looks similar to this,
{
tax_year: ['2016/17', '2017/18'],
status : ['completed'],
user : [1,4,78]
}
How would I go about search an array of objects and returning all the objects that have a matching key and value? I know I can do a single find with underscore like so,
var result = _.where(historyData, {tax_year: "2016/17"});
but I have no clue as to how to search for multiple matching keys and values?

It seems that you're matching on both of the values in an array value (regardless of whether there are more array values or not). If that's true, you might have to use the _.filter() method, where you can provide whatever logic you need in the 'predicate'. Alternatively, you could add a unique identifier on the server side to each record and match on that.

Related

How to efficiently query object with large field array?

So basically I have an object schema that has an array field that will hold ids for objects inside a different collection. This array field has the potential to have thousands of ids inside of it. I have been omitting this field using .select(["-fieldName"]); in my queries up till now but I need to include it in my query if I wanna add on to it.
I would assume querying an object with such a large field is costly in performance so therefore my question is how can I efficiently query such an object?
I would like to just omit this field in my query but then I can't add ids into it.
If you just want to add an item to the array field you can use the $push operator.
collection.updateOne(query, { $push: { arrayField: value } });

How can we just take the value of group names with vuejs?

I have an array checking for a REST API and which includes group names and group names include keys and values. I want to take just values of keys. Because keys in group names and values in keys. I just wanna see the values on the network side in properties.
I tried to use lodash components like a grubby,reduce,and omit etc. but ı couldnt make it work.

linqjs intersect comparer issue

I am using linqjs and I have one array full of ids to include in a list, and an array full of complex objects which have a property userId.
Problem is when I do an intersection it never seems to return anything, however there is very little information around the compareSelector.
So here is an example of what I am doing:
enumerableOfUsers.intersect(listOfIdsToInclude, "$.userId");
So in the above example enumerableOfUsers would be an existing enumerable created from an array of users (which contain the userId field), the listOfIdsToInclude is an array of id values, like ["12345", "213213", "2124"] etc.
The intersect seems to work but never returns anything and I know the userIds match so am I doing anything wrong here?
The thing is that the compare selector is applied to items of both the first and second sets. The second set is a list of ids already so the compare selector doesn't apply. The projection yields undefined values which will always result in no results found.
You need to apply the selector only to the first set of values. Try this instead:
// using linqjs 2.x syntax
var query = enumerableOfUsers.Select("$.userId").Intersect(listOfIdsToInclude);

Use array or JSON to keep track of unique list

I have an object in Parse .com database that can receive thumbs up votes. I want to keep track of which users have provided a thumbs up to prevent the same user doing it again. I am thinking of adding a field to my object called "voters" that will hold the list of voting users.
Should this be an array of usernames or a JSON with keys as the usernames?
Why would I use an array if i could use a json map and do O(1) lookup of the names on the keys?
ie object.voters[uername] = undefined then no votes?
Use Array if you need just the name of the voter .
Use Json if you need more info than just the name .
Why is that if u need just the name just making an array with the voter names first will be easy second it will be faster t process .
but if you need something like
voter={"name":"foo","age":"25"}
as you see we can use the powerof json to save multiple info about one user and anyway each voter must be within a single array so you can iterate over them
UPDATE---
to find the index of a name in an array just use array.indexOf("the name") what this will return is the index number of the name if the name doesnt exist it will return just -1 so a simple if else can handle this
UPDATE---
And here i found a performance test to compare which is faster to lookup whitin an array or an object and seems arrays still win this.
And here is another source for reading

Capturing form data: variables or array?

I have a form with about 20 input fields. I capture values of these fields, then do some calculations and output several values.
Is there a preferred/recommended way of capturing form data? Currently I store every form field into a separate variable. I was wondering if storing it to an array would be a better and more effective approach.
I'm quite new to Javascript and programming in general, trying to learn the best practices.
My best practice on this depends on what I have to do with the data. If I do not need to loop through it, or send it to another page/service, then there's nothing wrong with individual scoped-variables.
If I need to loop at all, I commonly use an array or object to loop through.
If I have to pass it to another page/service, I make one object variable to encapsulate the rest of them, so I can "stringify" it to JSON and parse back to an object on the other end.
Just my opinion,
Pete
You might consider the third approach - just use the data from the form without storing it anywhere.
First check the correctness, once it is considered correct just use what you have.
You should always assign the attribute "name=..." to an input element, so you can use something like:
var form = document.forms['form'];
var email = form['email'];
email = do something
if you use javascript... if you use jquery it's simple $('input[name="email"]') = do something
I prefer this way because you can call variables by name, not by number, for example "form[0] that corresponds to input[name="email"]".
Use the associative properties of arrays in JavaScript to get the benefits of unique field names and OOP:
var formModel = new Array();
formModel['myField'] = getMyFieldValue(); // make 'myField' index to any object
Then you can do things like:
formModel['myField'] // get the value
formModel.length; // number of fields added
for (entry in formModel) { /* loop thru entries */ }
formModel.sort([compareFunction]) // custom sort
formModel = []; // clear the model
Convert array to JSON
Any of these ArrayMDN conveniences.
Just one approach, but arrays in JS are extremely versatile and IMO underused objects in JS.

Categories

Resources