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);
Related
i've been googling around about how to add an object into an array in firestore, and found the arrayUnion() able to add an object into firestore array, but it only add the object into last index of array, but how to add it into first index of array?
//add "greater_virginia" into last index of array
washingtonRef.update({
regions: firebase.firestore.FieldValue.arrayUnion("greater_virginia")
});
//how to add "greater_virginia" into first index of array?
its basically same as arrayUnion but instead of add it into last index, i want to add it into first index of array.
If Firestore arrays behave anything like realtime-database arrays, then they don't actually exist. As far as I know, they are store as maps, like:
{
0: "first element",
2: "second and so on",
}
You can probably see how an unshift would be a big transformation. In fact, firestore doesn't let you do this, saying "...in order to avoid some of the issues that can arise in a multi-user environment, you'll be adding them with more of a set-like functionality".
With that in mind, this problem is usually solved at the application level by fetching the array, mutating it as needed, then setting the whole thing.
Bit of further reading https://firebase.googleblog.com/2018/08/better-arrays-in-cloud-firestore.html
PS: be careful with the arrayUnion operator, because it actually performs a add to set
Firestore doesn't offer any way to modify items of array fields by index. arrayUnion will only ever append to the end of the array if the element doesn't already exist.
If you want to modify an array by index, you will have to read the document, modify the array in memory to appear how you want, then write the modified array back to the document.
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.
I hope someone can explain this to me, in the undescorejs docs, it says:
Returns true if the value is present in the list. Uses indexOf internally, if list is an Array.
I want to see if the user is on the members page by checking if the path contains member. At the moment the path can be '/members/' or '/member/someid`.
So what I have at the moment is
_.contains(['/member'],path);
Which returns false in both cases, but if I check indexOf
path.indexOf('/member');
It returns 0 in both cases.
Why does _.contains seem to act differently than indexOf, and how can I fix it?
UPDATE:
I'm planning to test more pages than just member, that's why I'm using an array in contains()
contains searches a list for elements. If the list is ['a','b','c'] and you search for an element 'a' then you're going to succeed, and if you have a list ['abc','bcd','def'] and you search for an element 'a' you're going to fail, because that's not in the list. We can trivially write code to demonstrate this:
var list = [....];
var find = ...;
list.forEach(function(element) {
console.log("Is "+find+" the same as ["+element+"]? ", element==find);
});
In your example, is 'member' the same as '/member'? It very clearly isn't, they're different strings, and so contains() will report the right thing: your string is not contained in the list you're checking.
I'm building an Entity System for a game, and basically I'm not sure whether I should use simple objects (dictionaries) or arrays to to store the entities/components by their id.
My biggest issue is that I didn't want a dynamic entity id. If the id was just a string (using the dictionary to store entities), then it would always be valid and I could use storage[id] to get to that entity.
If I used arrays, I thought, the id's of entities, which would represent an index in the storage array, would change. Consider this array of entities:
[
Entity,
Entity, //This one is being removed.
Entity
];
If I was to remove the second entity in that array, I thought that the id required to access the third array would have to change to the id (index) of the (now removed) second entity. That's because I thought about deleting in terms of splice()ing.
But, I could use the delete expression to turn the element (an entity) into an undefined! And, if it's true that arrays in Javascript are actually just objects, and objects logically have infinitely many undefined values, does that mean that undefined values inside arrays don't use up memory?
Initially, I though that arrays were implemented in a way that they were aligned in memory, and that an index was just an offset from the first element, and by this logic I thought that undefined values would use at least the memory of a pointer (because I, actually, thought that pointers to elements are aligned, not elements themselves).
So, if I stored 10k+ entities in this array, and deleteed half of them, would the 5k undefined's use any memory at all?
Also, when I do a for entity in array loop, would these undefined elements be passed?
Also, where can I find resources to see how arrays are actually supposed to be implemented in Javascript? All I can find are general explanations of arrays and tutorials on how to use them, but I want to find out all about these little quirks that can prove important in certain situations. Something like a "Javascript quirks" site would be great.
Arrays are not just objects. In particular the length property is very magic.
Of course, a JavaScript engine is allowed to represent the array internally in any way it chooses, as long as the external API remains the same. For instance, if you set randomly separated values then they may be stored as a hash, but if you set consecutive values then they may be optimised into an array.
for ... in does not enumerate properties that are not set. This includes an array literal that skips values e.g. [true, , false], which will only enumerate indices 0 and 2.
The Linq-For-Javascript library contains functions that convert between "jQuery objects" and "Enumerable objects": toEnumerable() and TojQuery(). Consider the difference between these two lines:
$('tr'); // returns array of tr
$('tr').toEnumerable().TojQuery(); // returns array of tr[1]
Converting from jQuery to Enumerable and back to jQuery does not give you what you started with. The end result is an array of arrays of elements, with each sub-array having a length of 1. I do need to make use of Enumerable, so this is just a convenient example of my problem.
This means that to get the id of an element, you'd need to do the following:
$('tr')[0].id; // returns "myID"
$('tr').toEnumerable().TojQuery()[0][0].id; // returns "myID"
I'm surprised of this, because even though I've allegedly gone back TojQuery(), the object returned by TojQuery() does not work with typical jQuery calls:
$('tr').find('td').length; // returns 170 (in my case)
$('tr').toEnumerable().TojQuery().find('td').length; // returns 0 (BAD)
I would like it if both lines returned 170, but apparently Linq-For-Javascript doesn't work that way.
So, my questions:
Why is this?
Am I doing it wrong?
If not, any good workarounds? (convert array of 1-element arrays to array of elements?)
Thanks!
JQuery handles operations according to types. In the first line of the code, if finds all HTML TR objects and by the help of this information it can attach necessary functions to the found objects.
$('tr').find('td')
However, it could not understand after you change it to enumarable object since it is no longer seems to be a HTML Object instead it becomes any other type of object. Thus, jquery cannot attach a function to it.
$('tr').toEnumerable()