Array, Object and JSON is most widely used terms in Javascript. This posts explains in detail what is the difference between array, object and json. In simple words, arrays and objects are data structure to hold your data and JSON is the format to marshal(serialization) or unmarshal (deserialization) data.
Initialization of Arrays and Objects
// Array var arr = []; // This is basic syntax for array in javascript. typeof arr; // outputs object Array.isArray(arr); // outputs true arr instanceOf Array; // outputs true.
// Objects var obj = {}; // This represents objects in javascript. typeof obj; // outputs object.
If you have worked on java or c# then, You must be surprised that how come array and objects both are typeof objects? Well, In javascript, everything is typeof objects or one of six primitive data types (Boolean, null, undefined, number, string, and symbol). Use Array.isArray()
or instanceOf
method to check whether object is array or not.
Explore data structure of Arrays and Objects
Arrays are numbered indexes and objects are named indexes. Check out below examples to understand how arrays and objects supports different operations on data.
//Array
// Arrays are numbered indexes which can hold values
var arr = ["one", "two", 3];
// access array value using numbered index.
arr[0]; // Outputs one
// Push elements to end of array.
arr.push("four"); //arr will be ["one", "two", 3, "four"]
// Pop elements from end of array.
arr.pop(); // arr will be ["one", "two", 3]
// Get size of array.
arr.length; // will return 3 for arr.
// Insert into array at specific index
arr[3] = "four"; // arr will be ["one", "two", 3, "four"]
// Get index of element in the array
arr.indexOf("two"); // returns 1 as an index of "two" in the arr.
arr.indexOf("dummy"); // returns -1 as dummy is not in the arr.
// Sort array
arr.sort(); // sorts array. arr will be [3, "four", "one", "two"]
// Objects
// Objects are key - value pair data structure which can hold values for given key
var obj = {firstname : "Hiral", lastname : "Patel"};
// access object value using key.
obj.firstname; //returns hiral
// add another key-value in object
obj.middlename = "nareshkumar"; // adds middlename:"nareshkumar" in object key-value pair list
obj["country"] = "india"; //add country:"india" in object key-value pair list.
//remove element from object
delete obj.middlename; // deleted middlename key and its value from object key-value pair list.
// Get size of object
Object.keys(obj).length; // returns 3
// check key exists in object or not
obj.hasOwnProperty("firstname"); // returns true;
obj.hasOwnProperty("dummy"); // returns false;
JSON
You can convert your arrays and objects to/from a JSON string. Check out below example.
//Array
var arr = ["one", "two", "three", "four"];
//get JSON string from array.
var jsonStrofArray = JSON.stringify(arr); // returns json string of "["one","two","three","four"]"
// Recover array from JSON string
var arrFromJSONString = JSON.parse(jsonStrofArray); // Returns ["one", "two", "three", "four"]
//Objects
var obj = {firstname:"hiral", lastname: "patel"};
//get json string from object
var jsonStrofObj = JSON.stringify(obj); // returns json string of "{"firstname":"hiral","lastname":"patel"}"
// Recover object from JSON string
var objFromJSONString = JSON.parse(jsonStrofObj); // Returns json object with keys of firstname and lastname along with values.