I'm searching for a way to deal with a list of objects that should have unique IDs in Javascript. I want to be able to add, remove and edit (by their ID) objects that are in this list, loop over the list and be able to store it in a JSON file.
My naive implementation looks like this: [6,{"ID":4,"name":"test"},{"ID":5,"name":"test2"}]
Each time I add an object to the array I give it the number that is in position [0] as an ID and increase the number in position [0].
As I thought about how to edit an element in this kind of data structure (my ideas were using indexOf() or an ordinary for) I started to wonder whether my data structure was really the best way to solve my problem.
My question: Is there a better data structure/way of dealing with objects in JavaScript that fulfills my requirements?
Thanks in advance for the help
Related
This implementation from Geeks for Geeks strangely utilize array to implement bfs.
Link: https://www.geeksforgeeks.org/implementation-graph-javascript/
In the step "add the starting node to the queue", it even assigns "startingNode" as an index of the "visited" array, which sounds very off(it would make a bit more sense if it used object as the type). I was wondering if they incorrectly wrote this or if not, I'd like someone more experienced to explain this to me.
Thank you!
The traversal needs an efficient way to lookup a particular node in the visited set. That can be done efficiently with an array lookup such as if (!visited[neigh]). Another option would be using a string key and an object to represent the visited set, but it would likely be less efficient. Referring to the nodes as objects only would complicate things since there would be no natural key one could use to determine membership in the visited set.
Given someArr, (an array of php timestamps, which will later be converted to javascript timestamps (*=1000)), I was expecting lodashs...
_.sortedUniq(someArr)
to remove all duplicate values, giving the same result as
Array.from(new Set(someArr))
Could anyone explain why .sortedUniq() doesn't remove duplicates? I also tried _.uniq(), for that matter. Is my assumption wrong? Is there something wrong with my dataset?
Here's the mcve.
The question itself refers to after I define allVals, but I've left the way I'm constructing it in, just in case there's something wrong with how I'm doing that. The initial dataset array is what's coming from php and, for the time being, is not negotiable in terms of structure.
Please note that, while I do have a bit of exercise in javascript, I'm not a "schooled" programmer, I come from a design background and learned to code hands-on, so I'm not excluding the possibility that my grasp of certain programming patterns is not 100% accurate.
Thorough explanations are highly appreciated.
_.sortedUniq is designed for an array that's already been sorted. Your array has not been sorted.
Replacing it with _.uniq seems to work in removing the duplicates. (JSFiddle)
It is possible to remove duplicates from an array more efficiently if you know it has already been sorted. That, presumably, is why LoDash includes different functions for the two cases - sorted and non-sorted.
By the way uniqKeys === pointKeys will not correctly check whether the two arrays have the same contents, as the arrays are distinct objects even if they contain the same numbers. Instead you need to write a function to do this, or use isEqual.
According to lodash docs (https://lodash.com/docs/4.17.4#sortedUniq) _.sortedUniq is for sorted arrays, try using _.uniq()
Long story short : I'd like to treat several javascript associative arrays as a database (where the arrays are tables). The relations could be represented by special fields inside the arrays. I'm not interested in the persistence aspect of a database, I only want to be able to query the arrays with a SQL-like language and retrieve sets of data in the form of associative arrays.
My question : Is there any javascript library that has such features ? Otherwise, is there any library that can at least take care of the SQL-like language part ?
Thanks
I believe the closest thing to what you need is the jLinq library. It can operate with js objects and arrays much in the same way you would do with a database, but in a slightly different way. You don't really write queries, but use methods to construct them. Overall it's way better I think.
Some googling found this: http://ajaxian.com/archives/two-js-solutions-to-run-sql-like-statements-on-arrays-and-objects which seemed interesting.
Can I ask why you want to do this?
I came across this question while searching something sort of related. Wanted to share with you (9 years later, I do realize) that I have the same want/need, often, where the script I'm working on has a lot of cross-referencing to do between various sources of information. I use PowerShell. Enumerating arrays of objects, from within a loop, which is enumerating other objects, is just bad/slow/horrible.
To date, my solution has been to take all my arrays and then make hashtables from them, where the key/name is a property value that is common across all the arrays (e.g. ObjectId (GUID)), and the value is the entire object from the array. With this, while in my loop which is enumerating Array#1, I can check for the presence of this current item in any of the other arrays simply by checking the existence of the key in the corresponding hashtables, and that way there's no enumerating the other array, there's just direct , equal effort access to the correct item in the array (but really coming from the newly built hashtable).
So my arrays are just temporary collection buckets, then everything I do from there uses the hashtables, which are just index/lookup tables.
What I was searching for when I stumbled here, was for solutions to keep track of all the different hashtables in the building/planning phase of my scripts.
Here is something I would like to do, I have a file, which something like this:
"key"-"content", each "key" is not unique for the content, one key can have zero or more content that much....
The file is about 200Kb, I convert it in array, and put it all in the javascript. When user type, I loop the array once to find out the result, but it is slow...
Any suggestions on how to doing this? Thank you.
(Only client side javascript implementation is allowed, not allow to use server to analysis the result and send back.)
If I understood you right, these links to articles of John Resig may help you. His problem was the poor performance, when looking for valid words in a big text file while typing them.
Part 1: Dictionary Lookups in JavaScript
Part 2: JavaScript Trie Performance Analysis
I assume that the user is typing something that is supposed to match the "key"? Or the "content"?
Assuming it's the key, then sort the keys and use a binary search. Once you get a hit (assuming a partial match, like, say, the first letter), just keep scanning until your matches fail. That's your result set.
If you're querying the content, then it's the same premise, but you need to invert the index and make the content pieces your keys, and sort those.
Can you use an associative array, with unique keys that point to arrays of possible values?
{ 'key1' => ['value1','value2','value3'],
'key2' => ['value1','value2'],
'key3' => ['value1'],
}
It means more overhead to parse the list, but I bet searching the list will be much faster. It should also use less memory, since you aren't duplicating all of the duplicate keys in memory.
I am not really sure how to get data out of this nested object with Mustache.js. I am using the YQL multi query that brings back my data objects nested https://gist.github.com/866247. My problem comes when I am trying to access the nested objects
The data comes back like this where 0 and 1 are part of an array with objects in them. Here is a picture of the tree http://cl.ly/1e1b3O3U233e2I0d3g2f.
query
results
results
0
1
I have tried the template below and I don't get anything back. I believe the problem is that that second "results key" comes back with the array and mustache doesn't know what to do.
"{{#query}}
{{#results}}
{{#results}}
{{#photo}}
{{farm}}
{{#photo}}
{{/results}}
{{/results}}
{{/query}}"
If this makes sense to anyone, how do I access the nested objects in the array if there is no key to them?
I think you should flatten your data into a simpler format to write a template that makes sense, but you can get pretty close to what you want with it as-is.
The one insurmountable issue I saw was that 'results' is inside 'results' resulting in a tag closure mismatch. Doing something like this solves that problem:
data.query.flattened = data.query.results.results;
A functional sample is here.
You can find some more mustache tricks in this article.
This is one of the types of issues with Moustache that Handlebars attempts to address - traversing through objects in the template:
http://handlebars.strobeapp.com/#paths
Where you access it directly as if it's a directory structure. I know this isn't an answer to your question, per-se, but aside from flattening the data it's an alternative solution.