Found 9311 Articles for Object Oriented Programming

How can I filter JSON data with multiple objects?

AmitDiwan
Updated on 14-Sep-2020 08:40:38

16K+ Views

To filter JSON data with multiple objects, you can use the concept of filter along with ==.Exampleconst jsonObject= [    {       studentId:101,       studentName:"David"    },    {       studentId:102,       studentName:"Mike"    },    {       studentId:103,       studentName:"David"    },    {       studentId:104,       studentName:"Bob"    } ] var result=jsonObject.filter(obj=> obj.studentName == "David"); console.log(result);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo194.js. This will produce the following output −PS C:\Users\Amit\javascript-code> node demo194.js [    { studentId: 101, studentName: 'David' },    { studentId: 103, studentName: 'David' } ]

Display the Asian and American Date Time with Date Object in JavaScript

AmitDiwan
Updated on 14-Sep-2020 08:39:14

1K+ Views

For this, you can use timeZone from JavaScript i.e. specific time zones for Asia and America respectively.For Asian Time Zonevar todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"});For American Time Zonevar americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"});Examplevar todayDateTime = new Date().toLocaleString("en-US", {timeZone: "Asia/Kolkata"}); todayDateTime = new Date(todayDateTime); console.log("The Asia Date time is="); console.log(todayDateTime) var americaDateTime = new Date().toLocaleString("en-US", {timeZone: "America/New_York"}); americaDateTime = new Date(americaDateTime); console.log("The America Date time is="); console.log(americaDateTime);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo193.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo193.js The Asia Date time is= ... Read More

Display the dropdown’s (select) selected value on console in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:37:45

2K+ Views

Let’s say the following is our dropdown (select) −    Javascript    MySQL    MongoDB    Java Following is the code to display the selected value on Console −Example Live Demo Document Javascript MySQL MongoDB Java    function selectedSubjectName() {       var subjectIdNode = document.getElementById('subjectName');       var value =       subjectIdNode.options[subjectIdNode.selectedIndex].text;       console.log("The selected value=" + value);    } To run the above program, save the file name “anyName.html(index.html)” and right click on the file. Select the option ... Read More

How can we invoke the parent's method, when a child has a method with the same name in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:31:46

891 Views

In order to call the parent method when both parent and child have the same method name and signature.You can use the below syntax −console.log(yourParentClassName.prototype.yourMethodName.call(yourChildObjectName));Exampleclass Super {    constructor(value) {       this.value = value;    }    display() {       return `The Parent class value is= ${this.value}`;    } } class Child extends Super {    constructor(value1, value2) {       super(value1);       this.value2 = value2;    }    display() {       return `${super.display()}, The Child Class value2       is=${this.value2}`;    } } var childObject = new Child(10, 20); ... Read More

Length of a JavaScript object?

AmitDiwan
Updated on 14-Sep-2020 08:30:25

115 Views

Let’s say the following is our Student object −var studentObject = new Object(); studentObject["studentFirstName"] = "John"; studentObject["studentLastName"] = "Doe"; studentObject["studentAge"] = 22; studentObject["studentCountryName"] = "US"; studentObject["studentCollegeName"] = "MIT"; studentObject["studentSubjectName"] = "JavaScript";Let’s find the length.You can use the concept of keys available in object and if the key is present then increment the counter variable and return the counter after completing the for loop.Examplevar studentObject = new Object(); studentObject["studentFirstName"] = "John"; studentObject["studentLastName"] = "Doe"; studentObject["studentAge"] = 22; studentObject["studentCountryName"] = "US"; studentObject["studentCollegeName"] = "MIT"; studentObject["studentSubjectName"] = "JavaScript"; Object.findLength = function (stObject) {    var counter = 0, k;    for (k in ... Read More

How to combine two arrays into an array of objects in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:27:05

2K+ Views

Let’s say the following are our two arrays −var firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol'];To combine two arrays into an array of objects, use map() from JavaScript.Examplevar firstArray = ['John', 'David', 'Bob']; var secondArray = ['Mike', 'Sam', 'Carol']; var arrayOfObject = firstArray.map(function (value, index){    return [value, secondArray[index]] }); console.log("The First Array="); console.log(firstArray); console.log("The Second Array="); console.log(secondArray); console.log("The mix Of array object="); console.log(arrayOfObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo190.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo190.js The First Array= [ 'John', ... Read More

How to make my textfield empty after button click in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:24:19

2K+ Views

For this, you can use onclick=”yourFunctionName()”, and −document.getElementById(“”).value=’’Example Live Demo Document ClearInputText    function clearTheTextField(){       console.log("The text box       value="+document.getElementById('txtBox').value)       document.getElementById('txtBox').value = '';    } To run the above program, just save the file name anyName.html(index.html) and right click on the file and select the option open with live server in VS Code editor.OutputThis will produce the following output −Now enter some value into the text box and click the button ClearInputText.After entering the value, clicking the button.This will produce the following output −

Not able to push all elements of a stack into another stack using for loop in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:20:31

239 Views

As we know the stack works on the principle of Last in first out. At first, to insert into another stack you need to pop() all elements from the first stack and push into the second stack.Examplevar myFirstStack=[10, 20, 30, 40, 50, 60, 70]; var mySecondStack=[]; for(;myFirstStack.length;){    mySecondStack.push(myFirstStack.pop()); } console.log("After popping the all elements from the first stack="); console.log(myFirstStack); console.log("After pushing (inserting) all the elements into the second stack="); console.log(mySecondStack);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo189.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo189.js After popping ... Read More

Regex - reusing patterns to capture groups in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:19:00

454 Views

For this, use regular expression with digit along with $.Examplevar groupValues1 = "10 10 10"; var groupValues2 = "10 10 10 10"; var groupValues3 = "10 10"; var regularExpression = /^(\d+)(\s)\1\2\1$/; var isValidGroup1 = regularExpression.test(groupValues1); var isValidGroup2 = regularExpression.test(groupValues2); var isValidGroup3 = regularExpression.test(groupValues3); if(isValidGroup1==true)    console.log("This is a valid group="+groupValues1); else    console.log("This is not a valid group="+groupValues1); if(isValidGroup2==true)    console.log("This is a valid group="+groupValues2); else    console.log("This is not a valid group="+groupValues2); if(isValidGroup3==true)    console.log("This is a valid group="+groupValues3); else    console.log("This is not a valid group="+groupValues3);To run the above program, you need to use the following command −node ... Read More

Set scroll view with intervals in JavaScript

AmitDiwan
Updated on 14-Sep-2020 08:17:40

558 Views

For this, use the concept of scrollTop and scrollHeight.Example Live Demo Document    #scrollDemo {       height: 300px;       overflow-y: scroll;    }    #scrollDataFeatures {       height: 500px;       background-color: skyblue;    } See the below Message and Scroll UP    var scrollData = document.getElementById("scrollDemo");    scrollData.scrollTop = scrollData.scrollHeight    setInterval(() =>{       var heading3Data = document.createElement("h3");       heading3Data.innerHTML = "Scroll Down...Please Scroll UP"       scrollData.appendChild(heading3Data);       scrollData.scrollTop ... Read More

Advertisements