Found 9313 Articles for Object Oriented Programming

How to display only the current year in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:51:37

364 Views

To display only the current year, use getFullYear(). Following is the code −Example Live Demo Document The Current Year=    document.getElementById("theCurrentYear").innerHTML = new    Date().getFullYear(); 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 −

Strip quotes with JavaScript to convert into JSON object?

AmitDiwan
Updated on 07-Sep-2020 08:50:06

2K+ Views

For this, you can use replace() along with parse(). Following is the code −Examplevar studentDetails = `"{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}"`; console.log("The actual object="); console.log(studentDetails); var removeFirstAndLast = studentDetails.substring(1, studentDetails.length-1) var removeDoubleQuotes = removeFirstAndLast.replace(/""/gi, `"`) console.log(removeDoubleQuotes) var output = JSON.parse(removeDoubleQuotes); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo103.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo103.js The actual object= "{""name"": ""John"", ""subjectName"": ""Introduction To JavaScript""}" {"name": "John", "subjectName": "Introduction To JavaScript"} { name: 'John', subjectName: 'Introduction To JavaScript' }Read More

Copy objects with Object.assign() in JavaScript

AmitDiwan
Updated on 07-Sep-2020 08:48:54

125 Views

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

Is there a DOM function which deletes all elements between two elements in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:47:19

232 Views

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

Replace HTML div into text element with JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:45:24

713 Views

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 −

JavaScript Regex to remove leading zeroes from numbers?

AmitDiwan
Updated on 07-Sep-2020 08:42:56

870 Views

To remove leading zeros, use Regex in replace() method as in the below syntax −yourStringValue.replace(/\D|^0+/g, ""))Let’s say the following are our variables with number values −var theValue1="5001000"; var theValue2="100000005"; var theValue3="05000001"; var theValue4="00000000006456";Examplevar theValue1="5001000"; var theValue2="100000005"; var theValue3="05000001"; var theValue4="00000000006456"; console.log(theValue1.replace(/\D|^0+/g, "")); console.log(theValue2.replace(/\D|^0+/g, "")); console.log(theValue3.replace(/\D|^0+/g, "")); console.log(theValue4.replace(/\D|^0+/g, ""));To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo101.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo101.js 5001000 100000005 5000001 6456Read More

Update array of objects with JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:42:02

350 Views

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

How to add properties from one object into another without overwriting in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:40:22

560 Views

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

Assign new value to item in an array if it matches another item without looping in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:39:21

83 Views

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

How to disallow altering of object variables in JavaScript?

AmitDiwan
Updated on 07-Sep-2020 08:38:31

93 Views

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

Advertisements