Great two-part question. In general, the second question is non-trivial due to the complexity of the first.
Question 1:
what is an object, what is an array, and what is JSON. Can someone explain the syntax differences between the two?
Question 2:
and how to add elements to each,
Question 3:
how to combine each type, etc.
Answer 1:
This is a common stumbling block because JavaScript is more flexible than you might have expected at the beginning. Here is the curve.
In JavaScript, everything is an object.
So here is the code for each:
//What is an object? var obj = { }; var obj2 = { member:"value", myFunction:function(){} }
Above is an empty object. Then another object with a variable and function. They are called object literals.
Above is an empty array. Then another array with five integers.
Here is a curve that causes confusion.
//Get elements from each of the prior examples. var x = obj2["member"]; var y = array2[1];
What??? Do both objects and the array access the values ββwith a bracket? This is because both are objects. This proves to be good flexibility for writing advanced code. Arrays are objects.
// What is JSON?
JSON stands for JavaScript Object Notiation. As you might have guessed. All this is an object ... It is also { };
But this is different because - it is used to transfer data to - and - from JavaScript, and not to use (usually) in JavaScript. This is a file transfer format.
var JSONObject = {"member":"value"};
The only difference from the previous example is quotation marks. Essentially, we wrap the object literal as a string so that it can be transferred to or from the server, and it can be re-interpreted very easily. Better than XML because it does not need to be manually configured. Just call stringify()
or ParseJSON()
. Googling it. The fact is that ... JSON can be converted to a literal object of a JS object, and JS objects can be converted to JSON, for example, for transfer to a server or CouchDB database.
Sorry for the tangent.
Answer 2:
How to add an item to each? Here the curve ceases to be a nuisance and begins to be amazing! Since everything is an object, it is all about the same.
//Add to an object var obj {member1:"stringvalue"} obj.member2 = "addme"; //That is it! //Add to an array var array1 [1,2,3,4,5]; array1[0] = "addme"; array[6] = null; //We shouldn't mix strings, integers, and nulls in arrays, but this isn't a best-practice tutorial.
Remember the syntax of the JS object, and you can begin to see the whole new flexible world of objects. But it may take a bit.
Answer 3: Ah, yes ... how to merge.
There are serious (very many) ways to merge two arrays. It depends on what you need. Sorting, Duplication, Concatenation ... there are several.
Here is the answer !
UPDATE: how to make a beautiful multi-dimensional array.
//Multiple Dimension Array var array1 = [1,2,3]; var array2 = [3,4]; var arraysinArray = [array1,array2]; //That is it!
Here is the curve again, it may be in the object:
var obj{ array1:[1,2,3], array2:[3,4] }
JavaScript is powerful material that sticks to it; it gets good. :)
Hope this helps, all the best! Nash