What mathematical tool is similar to a JavaScript object? - javascript

I am trying to write documentation on a piece of Javascript code but I am having trouble describing the objects made by the code in a concise and understandable way. It is especially difficult because the objects have nested objects (often multiple layers).
Is there any mathematics that involves things with keys and attached values?
If not, how best can I describe an object with multiple nest objects in a concise manner?
Note: Just showing an example of an object is not enough as the structure changes often. Also, there are mathematical relationships between the keys and the values (coupon dates as keys and coupon payments as values).

I would say that Javascript objects are functions or mappings, in that they map keys to values.
Beyond that, it is hard to compare... the domain can encompass numbers, and a subset of all strings. As simple as that is to say, I'm not sure what mathematical field (etc) the domain would be equivalent to!
The range would, of course, be worse, as values in the range can be numbers, strings, booleans, undefined, further objects, or functions. However, I think the concept of an object being a mapping is fairly intuitive.
This doesn't include the prototype style inheritance, but I'm not sure how deep you want to go...

I saw a comment on it earlier, JavaScript objects pretty much follow the associative array abstract data type, which is a mathematical concept by virtue since computer science is basically a subset of applied mathematics, but if you need a true mathematical representation there's relational algebra which was created for relational databases (close enough) and is essentially an extension of set theory... just remember math doesn't necessarily mean it's clear and concise – Patrick Barr yesterday

Related

Is an array in JavaScript a pointers array? [duplicate]

This question already has answers here:
Are JavaScript Arrays actually implemented as arrays?
(2 answers)
How are JavaScript arrays implemented?
(8 answers)
Closed 2 years ago.
I am new to JavaScript and lately, I found out that arrays in JavaScript are like lists in Java and that they can contain different types of variables.
My question is if in JavaScript an array are made of pointers? How is it possible to have different types in the same array, because we must define the array size before we assign the variables?
I have tried to find some information on Google, but all I have found are examples on arrays ):
You do not have to define the array size before you assign the variables. You can go like:
let array = [];
array.push(12);
array.push("asd");
array.push({data:5});
array.forEach(element => {
console.log(element);
});
Also I think you should not think about pointers with such a high level language. The better way is to look at variables like 'primitives' and 'objects'. Here is a good read about it:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures
High level languages, and in particular scripting languages, tend to reference most things with pointers, and they make pointer access transparent. Javascript does this also. Most everything, even primitives like numbers and strings, are objects. Objects in javascript have properties that store things. Those properties are essentially pointers, in that they are references to other objects. Arrays are implemented in the same way, and are in fact objects with numeric properties (and a few utility methods a standard object doesn't have, such as .length, .push(), .map(), etc.). Arrays don't hav a fixed size anymore than objects do. So everything in javascript is stored in these object "buckets" that can store anything in their properties (although you can seal objects, like numbers and strings, so that they don't accidentally change).
Languages with fixed data types (C like languages for instance) implement things with fixed data structures, and the exact size is easily calculable and known. When you declare a variable, the compiler uses the type of that variable to reserve some space in memory. Javascript handles all that for you and doesn't assume anything is a fixed size, because it can't. The size of javascript objects can change at any time.
In C-Like languages, when you ask for an array, you are asking for a block of a specific size. The compiler needs to know how big that is so that it can determine where in memory to put everything, and it can use the type of objects in the array to easily calculate that. Interpreted languages use pointers behind the scenes to keep track of where everything is stored, because they can't assume it will always be in the same place, like a compiled program can. (This is somewhat of a simplification and there are caveats to this of course).
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array
JavaScript is a loosely typed language, Therefore there is noting stoping you from having different types in javascript array. but I would strongly avoid structuring your data that way without static type-checking (Typescript)
const test = ['test', {test:'test'}, 1, true]

Hashtable vs objects In javascript

I’m new to data structure and I’m learning it in Javascript.
My Question is:
Why do we need hash tables when we 've objects in javascript?
Can anybody give me a situation where hash tables will be more useful than objects?
"Hashtable" is called different things in different languages. Java has Hashtable and HashMap, Ruby has Hash, Python has dict... in JavaScript, it's called Map.
Objects' keys are limited to strings; Map keys can be anything.
Objects support inheritance; a Map only contains what is specifically put into it.
Think you means Map instead of HashTable. IMHO Map may be more useful and perform better if you need one of that:
keep order of insertions of key/value pairs;
frequent additional and removal;
key which not String/Symbol.
I think you can obtain more information at MDN
The MDN docs on this are quite helpful: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map#Objects_and_maps_compared
Most notably, using a map gives you the advantage of using anything as a key, maps retain order, and may perform better when constantly adding and removing values.

What are the semantic differences between `Set` and a `Map` in JavaScript?

It seems that everything you can do with Set you can do with Map? Is this correct?
What are the semantic differences between a Set and a Map?
Edit: the linked "dupe" does not enumerate the semantic differences between the two.
I have retracted my close vote.
A quick google of 'set vs. hashtable' or 'set vs. hashmap' turns up numerous SO questions, mostly in the Java tag, but I didn't see a single answer that actually tackled the difference in a good conceptual way (although a few linked to relevant resources).
Let's start with what data structures are: namely containers for values. The values can be whatever for the most part. Some data structures are homogeneous, some aren't. Some have restrictions (e.g. Map can have arbitrary keys but POJOs can only have string or symbol keys), some don't, some are ordered, some aren't, etc. All of these tradeoffs generally boil down to performance.
A Set is a data structure that holds unique values. Let's compare to an array:
Array.from(new Set([1,2,2,3])).toString() === [1,2,2,3].toString();
// false
Like arrays or lists, Sets in JavaScript are linear: you can traverse them in order*. But unlike arrays (more like lists) Sets are not indexed. You can't say new Set(1)[0];.
Maps on the other hand *ahem* map keys to values (indexed). If I have a Map new Map([['a',1]]), then .get('a') will return 1. Order is not generally considered important for Maps, that what key indexes are for. Nor is uniqueness: new Map([['a', 1], ['b', 1]]) stores the value 1 twice** and you can access it from either key.
Even if, like me, you are primarily a self-taught programmer, I highly recommend familiarizing yourself with basic data structures as it offers valuable insight into problem identification and general solutions. If you find yourself using Array.prototype.shift a lot for instance, you probably wanted a FIFO queue/linked list instead.
* Sets in general are unordered, the retention of insertion order is a JavaScript thing.
** The underlying implementation may as an optimization store it only once, but that is an implementation detail and opaque to you the user.

typed arrays vs arrays use cases webgl

I am a newbie with typed arrays. I looked at few docs online. But, confused - can someone explain in lamen terms?
When is typed array more useful than Arrays?
It seems typed arrays are useful when you want to build visualizations using WebGL. What about in a typical JavaScript web development?
Thanks
Typed arrays are more efficient, because they are limited to contain numbers (in contrast to normal arrays, which can contain elements of any type, even of different types in the same array). Typed arrays are useful when dealing with binary data (including images for visualisation), but are not commonly used elsewhere.

Javascript Data Structures for Efficient Insertion and Searching

I am in need of a Javascript data structure which will allow me to insert strings and search for strings efficiently. I have been looking around and the only data structures I have come across are objects and arrays. Objects are more used for encapsulation and cannot really be used for searching and using arrays can be slow. Are there any other data structures that will allow me to insert and search strings efficiently? Right now at best I could do a binary search on an array. Any other ideas? Thanks.
Objects are more used for encapsulation and cannot really be used for
searching
That was true in classical languages, not so true in JS.
var obj = { memberone: "value1" }
var value = obj["memberone"];
//value === "value1"
Objects can be searched in JS. Bear with me...
and using arrays can be slow.
Yes, can be - but don't have to be.
Are there any other data structures that will allow me to insert and
search strings efficiently?
Data structures? No. Again that is a classical perspective. In JS, it is different.
Check out _underscore.js.
It is 4k min gzip.
It provides a number of advanced iterator helpers (so you don't have to)
It provides templates to display your data to screen efficently.
It will benefit the rest of your development, maintenance, and implementations.
This is a good example of JS flexibility.
Hope that helps.
All the best!
Nash
There are some more I found after some goggling,
Javascript data structures - a collection object
One more thing you can use json objects and its JavaScript API to manipulate same. Please refer same here

Categories

Resources