Sharing array of nested objects between workers - javascript

I have an array of objects which contains more objects as values. I'm working with Web Workers and I need to use this exact array of objects (or another object) in worker file. Problem is I couldn't find any solution to forward this array or object from main file to worker.
I cannot use Shared Array Buffer because it needs Typed Arrays (or I couldn't find any solution). And stringifying doesn't work because after parsing the reserve array that keeping my objects which I am using to compare can not recognize object.

Two threads cannot share an array of objects. While it's possible to transfer arrays with some objects in them between threads (where the source thread loses access to those objects, until or unless the target thread sends them back), you can only do that with transferrable objects, and plain objects are not transferrable.
You'll need to use some kind of serialization turning the objects into strings or transferable objects, and then handle deserializing them.
And stringifying doesn't work because after parsing the reserve array that keeping my objects which I am using to compare can not recognize object.
You'll need to give them unique IDs that allow you to relate the serialized version back to the original.

Related

How does mongo retain order of an object when defining an index?

From the mongodb docs
Docs link
But in javascript you can't depend on the order of keys in an object 😱. So how does mongo reliably preserve the order of item and stock in the above example of a compound index?
No idea what the folks at MongoDB were smoking when they defined this API. As you have rightly pointed out, the JavaScript object only represents the unordered collection of key-value pairs both by design and spirit. For ordered collection, one could use array of objects.
The recent versions of JavaScript specifications encourage preserving the creation order of keys in the object for Object.keys and such methods. As others pointed out in the comments, relying on creation order of properties for the Object parameter for an API is plain stupid. It becomes even muddier if the object is cloned, the cloning library maintains the order or not. But probably it works since recent implementations now a days honors creation order and they pass the constant object inline.

Are arrays objects in JavaScript? [duplicate]

This question already has answers here:
Are Javascript arrays primitives? Strings? Objects?
(7 answers)
Closed 3 years ago.
I have had discussions with seasoned programmers who do not think arrays are objects; they think that objects and arrays are of separate types of the data structure category. But, as far as I understand, objects and arrays are of the same type.
If you were to use the unary typeof operator to evaluate a binding storing an array, the JavaScript interpreter will return the string "object." Moreover, you can use the Object.keys() method on a binding containing an array to get the property names of the array object.
When studying programming, I see authors use the phrase "objects and arrays," as if these were separate entities, which adds to the confusion. I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type.
As far as I understand it, objects and arrays are the same, except array objects are structured different than objects delimited by braces. Perhaps I'm missing something and can be enlightened.
Are arrays objects? If so, shouldn't they be listed as the same type, and shouldn't the phrase be "arrays and other objects"? If not, why not?
(Edit: I know the question of if objects and arrays are the same has been asked, but I was also asking if there should be a change in how arrays and objects are discussed, so people don't think that arrays are not objects, since programming is not the easiest subject to digest.)
Arrays are indeed a type of object.
console.log([] instanceof Object);
I go to W3Schools, and in the section "Data Types," they list arrays and objects as if they are not of the same data type
W3Schools is not a particularly trustworthy source. That said, when discussing organization of data, it's pretty common to talk about arrays (an ordered collection of values) as distinct from objects (a usually-unordered collection of key-value pairs), despite the fact that one is a subtype of the other, because the fact that arrays inherit from Object.prototype often isn't something one needs to consider when organizing / sorting through data.
If one tries to use any of the Object methods on an array, it's certainly essential to remember that Array inherits from Object, but usually that's not necessary - often, one will be only be using array methods instead (like forEach, find, includes, etc).

Is it possible to add an element after a set height interval?

I need to add an element like <div> end </div> every 100px. Is there any possible solution using Javascript, jQuery or php?
jsfiddle.net/vk4a7pwo/24 This is where my code is. I need the content of class .message-append put inside the .subpage class.
<div class="page">
<div class="subpage"></div>
</div>
<div class="page">
<div class="subpage"></div>
</div>
<div class="message-append"><p><strong>This article examines four new ES6 collections and the benefits they provide.</strong></p><p>Most major programming languages have several types of data collections. Python has lists, tuples, and dictionaries. Java has lists, sets, maps, queues. Ruby has hashes and arrays. JavaScript, up until now, had only arrays. Objects and arrays were the workhorses of JavaScript. ES6 introduces four new data structures that will add power and expressiveness to the language: </p><h2>Searching for the JavaScript HashMap</h2><p>HashMaps, dictionaries, and hashes are several ways that various programming languages store key/value pairs, and these data structures are optimized for fast retrieval.</p><p>In ES5, JavaScript objects — which are just arbitrary collections of properties with keys and values — can simulate hashes, but there are .</p><h3>Downside #1: Keys must be strings in ES5</h3><p>JavaScript object property keys must be strings, which limits their ability to serve as a collection of key/value pairs of varying data types. You can, of course, coerce/stringify other data types into strings, but this adds extra work.</p><h3>Downside #2: Objects are not inherently iterable</h3><p>Objects weren’t designed to be used as collections, and as a result there’s no efficient way to determine how many properties an object has. . When you loop over an object’s properties, you also get its prototype properties. You could add the property to all objects, but not all objects are meant to be used as collections. You could use the method, but this is just a workaround. When you loop over an object’s properties, the properties won’t necessarily be retrieved in the same order they were inserted.</p><h3>Downside #3: Challenges with built-in method collisions</h3><p>Objects have built-in methods like </p><p>ES6 includes new collection data types, so there’s no longer a need to use objects and live with their drawbacks.</p><h2>Using ES6 Map Collections</h2><p>is the first data structure/collection we’ll examine. Maps are collections of keys and values of any type. It’s easy to create new Maps, add/remove values, loop over keys/values and efficiently determine their size. Here are the crucial methods:</p></div>

LiveCycle: Load JSON data into pre-existing objects

A quick overview of the situation:
For simplicity of complicated data, I've got a series of JavaScript objects (built using function Object Factories) that handle auditing data, so for example, you have a child object inside a parent object inside a user object (these are basically arrays). These objects contain functions that help process the data they contain, both in and out, when in the UI.
On the top level, a single JSON function call pulls the data the objects contain out, without the functions (which is what I want to happen, as only the data is stored in an external database).
The problem
However, on pulling the information back from the database, using JSON.parse and assigning the result into the top level object holder obviously only contains just the data, no functions.
Bearing in mind the object arrays won't be initialised (as they won't know the length until extracted from the JSON data), how can I reinsert the JSON data back into the objects, functions included?
Is there some way of saying to the parser 'for this level of data, use this object, for this level, use this one'?
[Note: due to the nature of the organisation and security, external libraries aren't permitted. No jQuery suggestions etc.]

Is it possible to disable Firebase's auto-conversion to native arrays?

In the Firebase documentation, it says:
Firebase stores all data as Objects, even Arrays are stored as objects with numerical keys.
As a convenience, the Firebase Web API automatically converts Array-like Objects into Arrays
for use JavaScript.
This "convenience" is a royal pain, in my opinion. Is there any way to disable the automatic conversion, while still using array-like objects with numerical keys? I am using the arrayjs library, and would like to maintain those array-like objects throughout the application.
It also says in the docs: "It's not currently possible to change or prevent this behavior." So no, you can't disable it.
You can work around it as follows:
add any non-numeric key to the path (e.g. "ignoreme": true), which will prevent any keys from being treated as numeric
prefix the item keys with a string (e.g. "rec1", "rec2"...)
make the numbers non-consecutive
But in reality, sequential numeric ids in distributed real-time data are ill advised and should be avoided for most cases. They cause nothing but heartache.

Categories

Resources