Found 10483 Articles for Web Development

Convert buffer to readable string in JavaScript?

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

729 Views

For this, use the concept of toString(‘utf8’). Following is the code −In the code below, there are detailed explanations about the buffer.Examplevar actualBufferObject = Buffer.from('[John Smith]', 'utf8') console.log("The actual buffer object="); console.log(JSON.stringify(actualBufferObject)) console.log("Get back the original object="); console.log(actualBufferObject.toString('utf8')); var myObjectValue = '[John Smith]'; console.log("The data you are getting from the buffer is equal to ASCII code equivalent...") for (var counter = 0; counter < myObjectValue.length; counter++) {    console.log("The ascii value of " + myObjectValue[counter] + " is ="    + (myObjectValue.charCodeAt(counter))); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is ... Read More

How to avoid inserting NULL values to a table with JavaScript?

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

607 Views

In order to get rid of null values inserted into a table, you need to check the condition while entering the value.The condition to check NULL must be as follows −while( !( yourVariableName1==null || yourVariableName2==null || yourVariableName3==null…...N){    // yourStatement1    .    .    N }The above logic will never allow inserting the null values.Now you can use for loop and insert value into the table without NULL. Following is the code −Example Live Demo Document Demo Of Inserting the value into the table This is demo on the javascript ... Read More

Remove values in an array by comparing the items 0th index in JavaScript?

AmitDiwan
Updated on 14-Sep-2020 08:47:28

252 Views

Let’s say the following is our array &mius;var subjectNameAlongWithMarks = [    ["JavaScript", 78],    ["Java", 56],    ["JavaScript", 58],    ["MySQL", 77],    ["MongoDB", 75],    ["Java", 98] ]Above, we have repeated values, which we need to remove by comparing the duplicate values 0th index. Use the concept of Set() from JavaScript for this −Examplevar subjectNameAlongWithMarks = [    ["JavaScript", 78],    ["Java", 56],    ["JavaScript", 58],    ["MySQL", 77],    ["MongoDB", 75],    ["Java", 98] ] var distinctResult = subjectNameAlongWithMarks.filter(function ([value]){    return !this.has(value) && !!this.add(value) }, new Set()) console.log(distinctResult);To run the above program, you need to use ... Read More

Multi-selection of Checkboxes on button click in jQuery?

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

463 Views

For this, use jQuery() with id property. Following is the code −Example Live Demo Document    .changeColor {       color: red    }; Javascript MySQL MongoDB Python    jQuery("#selectDemo").click(function () {       jQuery(this).toggleClass("changeColor");       if (jQuery(this).hasClass("changeColor")) {          jQuery(".isSelected").prop("checked", true);          jQuery(this).val("Want To UnSelect All Values");       } else {   ... Read More

Can JavaScript parent and child classes have a method with the same name?

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

1K+ Views

Yes, parent and child classes can have a method with the same name.Exampleclass Parent {    constructor(parentValue) {       this.parentValue = parentValue;    }    //Parent class method name which is same as Child Class method name.    showValues() {       console.log("The parent method is called.....");       console.log("the value is="+this.parentValue);    } } class Child extends Parent {    constructor(parentValue, childValue){       super(parentValue);       this.childValue = childValue;    }    //Child class method name which is same as Parent Class method name.    showValues() {       console.log("The child ... Read More

How can I filter JSON data with multiple objects?

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

17K+ 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

2K+ 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

3K+ 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

1K+ 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

194 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

Advertisements