Loading....

JSON => Javascript Object Notation

JSON format is shown below
    {
        "Cars":[
            {},{},{},{array of objects}
         ]
    }
                    


Array in javascript

Array Of Numbers
    var arrNumbers=[1,2,4,6,7,8];
    alert(arrNumbers.filter(no => (no> 4 && no<8 )))
    alert((arrNumbers.filter(no=> (no > 4 && no < 8))).join(";"))
                        

Object
    var car = {Brand:"Audi",Color:"red",type:"Sedan"}     =>  This is object of Car((arrNumbers.filter(no => (no > 4 && no < 8))).join(";"))
                    


Array Of Object (push/unshift/splice/find/filter)

=> is called as arrow function or has rocket used below in filter or find
    var arrCars =  [{ Brand: "Merc", Color: "black", type: "Sedan" },
                    { Brand: "Mahindra", Color: "red", type: "Suv" },
                    { Brand: "Mahindra", Color: "white", type: "Suv" }] 

    var car =  { Brand: "Audi", Color: "red", type: "Sedan" }


    arrCars.push(car)       ==> This push car object at the last of Array


    arrCars.unshift(car)    ==> This push car object at the beginning of Array


    arrCars.splice(car)     ==> This push car object at the beginning of Array


    arrCars.splice(1,0,car) ==> {index To Start, no of Objects to delete, object to add}


    alert(JSON.stringify(arrCars.find(ca =>     (ca.Color=="red" )    )))      => Find  just the first occurance / return first matching object


    alert(JSON.stringify(arrCars.filter(ca =>    ca.Color=="red"      )))   
                 OR
    alert(JSON.stringify(arrCars.filter(ca =>    ca.Color==="red"     )))
                 OR
    alert(JSON.stringify(arrCars.filter(ca =>   (ca.Color==="red" )   )))
    alert(JSON.stringify(arrCars))
 


Update Array of Object
    <%-- arrCars.map(car=>car.type="Sedan" );--%>
  arrCars.filter(x => x.Brand == "Mahindra").map(ob => ob.Color = "Black")
                       

Sum of Column
      var arrInvoices = [{ InvNo: "232", Amount: "45", TotalNet: 45 },
                        { InvNo: "4545", Amount: "65", TotalNet: 65 },
                        { InvNo: "5656", Amount: "9", TotalNet: 9 }] 
        console.log(arrInvoices.reduce((n, { Amount }) => n + parseFloat(Amount), 0))
        console.log(arrInvoices.reduce((n, { TotalNet }) => n +TotalNet , 0))// If Column name is already in float
                       


Loop In Object
      var car = { Brand: "Audi", Color: "red", type: "Sedan" } 
      for (var i in car) {
          alert(i + ";" + car[i])             // ==> i is property name & car[i] gives value
      }

Loop In Array
    var arrCars =  [{ Brand: "Merc", Color: "black", type: "Sedan" },
                    { Brand: "Mahindra", Color: "red", type: "Suv" },
                    { Brand: "Mahindra", Color: "white", type: "Suv" }]

    var car = { Brand: "Audi", Color: "red", type: "Sedan" }
    arrCars.push(car) 
    for (var i in arrCars) {
        if (arrCars[i].Color == "red") { alert(arrCars[i].Brand) }
                      OR
        if (arrCars[i]["Color"] == "red") { alert(arrCars[i]["Brand"]) }
    } 
                            OR
    arrCars.forEach(function(ob) {
      if (ob.Color === "red") {
          alert(ob.Brand);
      }

Create Array from Existing Array
    var arrCars = [{ Brand: "Merc", Color: "black", type: "Sedan" },
                { Brand: "Mahindra", Color: "red", type: "Suv" },
                { Brand: "Mahindra", Color: "white", type: "Suv" }]
    var arrCarsModified = arrCars;  // This will always make changes in both array. It works Like pointer.
                 
    -------Create New Array that will not change in parent array-----------
    var arrCarsModified = arrCars.map(item => ({ ...item }));
                OR
    var arrCarsModified = new Array();
    arrCars.forEach(function(ob) {
       var objThis=Object.assign({}, ob)
       arrCarsModified.push(objThis)
    }
		OR
    var arrCarsModified = JSON.parse(JSON.stringify(arrCars));


Get disitnct Value in array from Array Of objects
var arrCars = [{ Brand: "Merc", Color: "black", type: "Sedan", amount: 2000 }, 
               { Brand: "Mahindra", Color: "red", type: "Suv", amount: 54000 }, 
               { Brand: "Mahindra", Color: "red", type: "Suv", amount: 54000 }, 
              { Brand: "Mahindra", Color: "white", type: "Suv", amount: 23000 }];
var distinctBrand = Array.from(new Set(arrCars.map(ob => ob.Brand)))  

var cars=arrCars.map(ob => ob.Brand)  //all Brand repeated in Array
var carsSet=new Set(arrCars.map(ob => ob.Brand))  //all distinct Brand of set
var arrCars=Array.from(new Set(arrCars.map(ob => ob.Brand)))  // convert Set to array
 


Get Maximum Value in array from Array Of objects
var arrCars = [{ Brand: "Merc", Color: "black", type: "Sedan", amount: 2000 }, 
               { Brand: "Mahindra", Color: "red", type: "Suv", amount: 54000 }, 
               { Brand: "Mahindra", Color: "red", type: "Suv", amount: 54000 }, 
              { Brand: "Mahindra", Color: "white", type: "Suv", amount: 23000 }];

var maxAmount = Math.max(...arrCars.map(car => car.amount));

//array.reduce(callbackFunction(accumulator, currentValue, currentIndex, array), initialValue)

var maxAmount = arrCars.reduce(function (accumulator, currentValue, currentIndex, array) { 
    return currentValue.amount > accumulator ? currentValue.amount  : accumulator;
}, 0)// mainly used when we want to return object as shown below this

var maxAmountObj = arrCars.reduce(function (accumulator, currentValue, currentIndex, array) { 
    return currentValue.amount > accumulator.amount ? currentValue : accumulator;
})// mainly used when we want to return object 
alert(JSON.stringify(maxAmountObj))


Array of multiple array & manipulating this
var data = {
    "County": [
        {"id": 1,"name": "India"},
        {"id": 2,"name": "USA"}
    ],
    "Roles": [
        {"id":1,"name": "Admin" },
        {"id":2,"name": "Technician"},
        {"id": 3,"name": "Sales Manager"}
    ],
    "layourVersion":"2"
};

--Update role name to "Super Admin" where id =1 & Create if it doesn't exists--
if (data.Roles) {
    var role = data.Roles.find(x => x.id === 1);
    if (role) {
        role.name = "Super Admin";
    }
   else  data.Roles.push({id: 1, name: "Super Admin"});
}

--update   layoutVersion to 3--
data.layoutVersion = "3";

--Add new array of object --
data["State"] = [
    { id: 1, name: "UK" },
    { id: 2, name: "AK" }
];

--Append new object in Country --
data.County.push({id: 3, name: "Canada"});


Last Update: July 17, 2026  

July 9, 2026 29 vikas@crmhike.com  JS
Total 0 Votes:
0

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Add A Knowledge Base Question !

You will receive an email when your question will be answered.

+ = Verify Human or Spambot ?

Back To Top

Add A Knowledge Base Question !

You will receive an email when your question will be answered.

+ = Verify Human or Spambot ?