Following is the code to merge two JavaScript objects together −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Merge two JavaScript objects CLICK HERE Click the above button to merge person and adress object together. let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let person = { name: "Rohan", age: 22, }; let address = { state: "Delhi", country: "India", }; let mergedObj = { ...person, ...address }; BtnEle.addEventListener("click", () => { for (let i in mergedObj) { resEle.innerHTML += "Key = " + i + " : Value = " + mergedObj[i] + ""; } }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −
Following is the code to replace array of object property name in JavaScript −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Replacing array of object property name CLICK HERE Click the above button to change the property name of an object in array let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); var person = [ ... Read More
Following is the code to find elements of JavaScript array by multiple values −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Find elements of JavaScript array by multiple values CLICK HERE Click the above button to see if arr contains all elements of arr1 or not let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let arr = ... Read More
Following is the code to convert array of comma separated strings to array of objects −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result, .sample { font-size: 18px; font-weight: 500; color: rebeccapurple; } .result { color: red; } Convert array of comma separated strings to array of object CLICK HERE Click the above button to convert the above array of strings to objects ... Read More
Following is the code to convert a string to JavaScript object −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample { font-size: 18px; font-weight: 500; color: red; } convert a string to JavaScript object {"name":"Rohan", "sports":["Cricket", "Football"], "country":"India"} CLICK HERE Click on the above button to convert the above string to JavaScript object let sampleEle = document.querySelector(".sample"); let resultEle = document.querySelector(".result"); let parsedJson = ... Read More
In JavaScript the primitive like string, number, boolean etc are compared by their values while objects (native or custom) are compared by their reference. Comparing by reference means whether the two or more object point to same location in memory or not.Following is the code to explain equality of objects in JavaScript −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Equality of ... Read More
Following is the code to parse a string from a JavaScript array −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Parse a string from a JavaScript array CLICK HERE Click the above button to convert the array arr to string let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let arr = ["Cow", "Lion", "Tiger", "Dog", "Cat"]; BtnEle.addEventListener("click", () => { resEle.innerHTML = arr.toString(); }); OutputThe above code will produce the following output −On clicking the ‘CLICK HERE’ button −
Following is the code for deep cloning an object in JavaScript −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Deep cloning an object in javascript CLICK HERE Click the above button to deep clone the obj object let BtnEle = document.querySelector(".Btn"); let resEle = document.querySelector(".result"); let obj = { firstName: "Rohan", ... Read More
Following is the code for using methods of array on array of JavaScript objects −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .result { font-size: 18px; font-weight: 500; color: rebeccapurple; } Using methods of array on array of JavaScript objects CLICK HERE Click the above button to apply array methods on array of objects let BtnEle = document.querySelector(".Btn"); let arr = [ { ... Read More
Following is the code to convert dictionary into list of JavaScript objects −Example Live Demo Document body { font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif; } .sample{ font-size: 18px; font-weight: 500; color: rebeccapurple; } Convert dictionary into list of JavaScript objects { A: { 1: "Apple", 2: "Apricot" }, B: { 1: "Ball", 2: "Bull" }, C: { 1: "Cat", 2: "Cow" }, D: { 1: "Dog", 2: "Drill" }, } CLICK HERE Click the ... Read More