Wednesday, July 31, 2019

Javascript - Array vs JSON vs Object basics with Examples

Array, JSON, Objects are valid objects used to interact and manipulate data in javascript applications. This post talks about the difference between an array, JSON and objects with examples.

Javascript Object Basics examples

Objects are the main building blocks of Object-oriented programming concepts. Objects can be used to carry the data in applications. There are predefined objects as well as can declared user-defined objects. Document and window are predefined objects in javascript. Objects can contain properties and functions. The object can also be used with an instance or object name Objects can be created in many ways, One way is using new operator and other is using object literal syntax.

Declare, initialize the object using new operator and constructor

The below is an example create an empty object using new operator with Object constructor. new is a keyword and operator to create an instance of an object. We can create an instance of the object using new operator followed by Constructor.
var firstObject = new Object(); 
console.log(firstObject) // {}
The above code creates an object and initializes with an empty object.

Declare, initialize the object using Object literal

An empty object can also be created and initialized with object literals. Object literals are enclosed in curly braces with data
var firstObject = {}; 
console.log(firstObject) // {}
or
object can be created with object literals data
var obj={"kiran":"test"}
This is preferred the way of creating object

declare assign properties to javascript object

javascript object contains properties. each property can be primitive type - Boolean, null, undefined, number, string, and symbol or any object
var firstObject = {}; 
firstObject.id = 1;     
firstObject.name  = "JOHN";
console.log(firstObject) // {id: 1, name: "JOHN"}

access object value using properties

Object data can be retrieved using property name with an object. property syntax.
Following is an example
var obj={id: 1, name: "JOHN"}
console.log(obj.id) // 1
console.log(obj.name) // "JOHN"

Size of an object in javascript 

Object size is a count of properties in an object. Following is an example for finding keys of an object.
var obj={id: 1, name: "JOHN"}
console.log(Object.keys(obj).length) // 2

how to check key exists in an object or not 

key can be checked in an object using hasOwnProperty method
var obj={id: 1, name: "JOHN"}
obj.hasOwnProperty("id"); //true;
obj.hasOwnProperty("id"); //false;

JSON Object examples in javascript

JSON is a javascript object notation used to transfer data between client and server. It contains key and values of javascript data types. This is useful in many programming languages to interact with REST API JSON data contains key and value pairs.
Here is a sample JSON object in javascript
{
"name": "Kiran",
"salary":2000
}

Initialize and Create a JSON object 

There are many ways we can create a JSON object in javascript.
Empty Object Creation
var JsonObject={};
or JSON object can be created using object
var jsonObject=new object();
JSON object always created using curly braces Initialize JSON object with key and values
var jsonObject={"name":"kiran","id":1}
The above object is created with name and key-value pairs like javascript object and easy key and value pair is separated by comma Create JSON Object Arrays Arrays in javascript can be written using square brackets which contain collection of objects
var jsonObject={"name":"kiran","id":1,depts:["dept1":"HR","dept":"admin"]}
The above example JSON object contains depts array of two objects. Convert JSON string to javascript object with examples Json text can be converted to javascript object using JSON.parse() method. parse() method takes json string and returns Javascript object, if invalid json string is passed, returns empty object string how to check key exists in object or not key can be existed in objet using hasOwnProperty method
var jsonString = ‘{ "name":"kiran",salary:1000}’;
var javascriptObject = JSON.parse(jsonString);
console.log(javascriptObject.name)// kiran
console.log(javascriptObject.salary)// 1000
Convert JSON to String using stringify method JSON.stringify() function is used in following cases convert json into String
var obj = { name: "kiran", salary: 2000 };
var json = JSON.stringify(obj);
arrays can also be stringfied using Stringify method
var myarray = [ "Ram", "Fran", "Sai"];
var myjson = JSON.stringify(myarray);
Array in javascript with examples Array are collectino of elements with index order. Declare and initalize array Array can be decla Kiran

Related article

This Is The Newest Post


EmoticonEmoticon