The Object.assign() method is used to copy one or more source objects to a target object. It invokes getters and setters since it uses both 'get' on the source and 'Set' on the target.The syntax is as follows −Object.assign(target, ...source objects);Following is the code to copy object −Examplevar object = {first: second => second + 1000} var object2= Object.assign({}, object); console.log("The result="); console.log(object2.first(1000));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo102.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo102.js The result= 2000Read More
Let’s say the following are our elements −My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol ENDWe need to remove theelement and its content. Theelements are in between the START and END.To remove elements between two elements, use the concept of remove(). Following is the code −Example Live Demo Document START My Name is John My Name is David My Name is Bob My Name is Mike My Name is Carol END const startingPoint = document.querySelector("nav"); const endingPoint = document.querySelector("footer"); ... Read More
For this, use document.querySelectorAll(). With that, also use the getElementsByClassName(). Following is the code −Example Live Demo Document My Name is John My h6 value must be here... My h6 value must be here... My h6 value must be here... const allSpan = document.querySelectorAll('.my-main-div-class span'), repaceAboveText = document.getElementsByClassName('uniqueId')[0].textContent; for (var newElement of allSpan){ newElement.textContent=repaceAboveText } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option “Open with Live Server” in VS Code editor.OutputThis will produce the following output −
Let’s say the following are our array of objects −var studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C'] }]We need to add the following in the already created array of objects −{firstName: "Bob", listOfSubject: ['JavaScript']};Examplevar studentDetails = [ { firstName: "John", listOfSubject: ['MySQL', 'MongoDB']}, {firstName: "David", listOfSubject: ['Java', 'C']}]; updateThisObject = {firstName: "Bob", listOfSubject: ['JavaScript']}; function forLoopExample(studentObjects, updateObj){ for(var index = 0;index < studentObjects.length; index++) { if(updateObj.listOfSubject.join("") === studentObjects[index].listOfSubject.join("")) { studentObjects[index] = updateObj; ... Read More
Let’s say the following are our objects −var first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000}You can use the concept of hasOwnProperty() to add properties from one object to another. Following is the code −Examplevar first = {key1: 100, key2: 40, key3: 70} var second = {key2: 80, key3: 70, key4: 1000} function addPropertiesWithoutOverwritting(first, second) { for (var key2 in second) { if (second.hasOwnProperty(key2) && !first.hasOwnProperty(key2)) { first[key2] = second[key2]; } } return first; } console.log(addPropertiesWithoutOverwritting(first, second))To run ... Read More
For this, use filter() along with map(). Let’s say the following is our array −const studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ]We will assign a new value to the name “Bob”. Following is the code −Exampleconst studentDetails = [ {Name: "John"}, {Name: "David"}, {Name: "Bob"}, {Name: "Mike"} ] var changeName = "Bob"; studentDetails.filter((obj) => obj.Name === changeName).map((obj) => obj.Name = "Carol"); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo98.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> ... Read More
Use the concept of freeze() from JavaScript to disallow adding new properties to an object, altering of object properties, etc.Following is the code wherein we are changing the value but the previous value still remains since we cannot alter properties using freeze() −Exampleconst canNotChangeTheFieldValueAfterFreeze = {value1 : 10, value2: 20 }; Object.freeze(canNotChangeTheFieldValueAfterFreeze); canNotChangeTheFieldValueAfterFreeze.value = 100; console.log("After changing the field value1 from 10 to 100 ="+canNotChangeTheFieldValueAfterFreeze.value1);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo97.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo97.js After changing the field value1 from 10 ... Read More
Let us first create a nested object −var details = { "teacherDetails": { "teacherName": ["John", "David"] }, "subjectDetails": { "subjectName": ["MongoDB", "Java"] } }Let us now extract the keys. Following is the code −Examplevar details = { "teacherDetails": { "teacherName": ["John", "David"] }, "subjectDetails": { "subjectName": ["MongoDB", "Java"] } } var objectName, nestedObject; var name = "Java"; for(var key in details){ for(var secondKey in details[key]){ if(details[key][secondKey].includes(name)){ objectName = ... Read More
To set sleep i.e. delay, use the concept of setTimeout(). It takes values in milliseconds i.e.1000 milliseconds = 1 second 2000 milliseconds = 2 seconds, etc.For our example, we will add two values with a delay of 5 seconds i.e. 5000 milliseconds. Following is the code −Examplevar firstValue=10; var secondValue=20; var result=firstValue+secondValue; setTimeout(function() { }, (5 * 1000)); console.log("The result="+result);The above setTimeout() will sleep for 5 seconds. To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo95.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo95.js The result=30Read More
To get largest numbers passed using reduce(), use the Math.max() function. Following is the code −const getBiggestNumberFromArraysPassed = allArrays => allArrays.reduce( (maxValue, maxCurrent) => maxValue.push(Math.max(...maxCurrent)),maxValue),[]); console.log(getBiggestNumberFromArraysPassed([[45,78,3,1],[50,34,90,89],[32,10,90,99]]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo94.jsOutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo94.js [ 78, 90, 99 ]