Found 6710 Articles for Javascript

How do I display only the visible text with jQuery?

AmitDiwan
Updated on 01-Sep-2020 11:34:16

525 Views

To display only the visible text, use the concept of − visible selector in jQuery. It selects the element currently visible. Following is the code −Example Live Demo Document Test Class Demo class    $('#myDiv').children(":visible").text() 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.This will produce the following output displaying the visible text −

Parse JSON in JavaScript to display a specific name/value pair?

AmitDiwan
Updated on 01-Sep-2020 11:32:25

575 Views

To parse JSON, use parseJSON() and for displaying a specific pair, use the $.each() function.Let’s say the following is our JSON, which is parsed −const APIData = '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", "Age" :20}]'; const getObject = jQuery.parseJSON(APIData);Now, let’s see the complete code and fetch the “Name” pairs −Example Live Demo Document    const APIData =    '[{"Name":"John", "Age":21}, {"Name":"David", "Age":24}, {"Name":"Bob", "Age":20}]';    const getObject = jQuery.parseJSON(APIData);    $.each(getObject, function (k, obj) {       console.log(obj['Name']);    }); To run the above program, save the file name ... Read More

How to find all subsets of a set in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:30:22

677 Views

To find all subsets of a set, use reduce() along with map() in JavaScript. Let’s say, we are passing the set [8,9] and finding the subsets.Exampleconst findAllSubsetsoOfGivenSet = originalArrayValue => originalArrayValue.reduce(    (givenSet, setValue) => givenSet.concat(       givenSet.map(givenSet => [setValue,...givenSet])    ), [[]] ); console.log(findAllSubsetsoOfGivenSet([8,9]));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo36.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo36.js [ [], [ 8 ], [ 9 ], [ 9, 8 ] ]

How to convert an object into an array in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:28:29

406 Views

To convert an object into an array, use push() in JavaScript. Following is the code −ExamplestudentDetails=   [       { studentName:"Chris",studentMarks:34},       { studentName:"David",studentMarks:89} ] var convertIntoArray = []; for (var i = 0; i < studentDetails.length; i++) {    convertIntoArray.push(studentDetails[i].studentName); } console.log(convertIntoArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo35.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo35.js [ 'Chris', 'David' ]

How can I cut a string after X characters in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:26:38

701 Views

To cut a string after X characters, use substr() function from JavaScript. Following is the JavaScript code wherein we are cutting the string after 9th character −Examplevar myName="JohnSmithMITUS"; console.log("The String="+myName) var afterXCharacter = myName.substr(0, 9) + "\u0026"; console.log("After cutting the characters the string="+afterXCharacter);Above, the unicode “\u0026” will replace all the characters with &(“\u0026”).To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo34.js.OutputThis will produce the following output.PS C:\Users\Amit\JavaScript-code> node demo34.js The String=JohnSmithMITUS After cutting the characters the string=JohnSmith&Read More

Finding content of arrays on the basis of specific property in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:25:40

144 Views

For this, you can use find() along with a map(). Let’s say we have student records with name, rollno and subject.Examplevar firstObject= [    { "FirstName": "David", "RollNo": "105", "Subject": "MongoDB" },    { "FirstName": "Mike", "RollNo": "110", "Subject": "JavaScript"} ]; var secondObject= [    { "FirstName": "Bob", "RollNo": "101", "Subject": "Java" },    { "FirstName": "John", "RollNo": "110", "Subject": "MySQL" } ]; var output = firstObject.map(first=>(secondObject.find(second=>second.RollNo==first.R ollNo) || first)); console.log(output);To run the above program, you need to use the following command −node fileName.jsHere, my file name is demo33.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo33.js [   ... Read More

How can we separate the special characters in JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:23:53

320 Views

To separate the special character, use the concept of match() with Regular Expression. The syntax is as follows −yourStringName.flatMap(anyVariableName => yourVariableName.match(/\w+|\W+/g));Let’s say, the following is our array with special characters in between values −var allNames = ['John-Smith', 'David', 'Carol%Taylor'];Let’s now see how to separate text with special characters. Following is the code −Examplevar allNames = ['John-Smith', 'David', 'Carol%Taylor']; var output = allNames.flatMap(obj => obj.match(/\w+|\W+/g)); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo32.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo32.js [    'John', '-',    'Smith', 'David',   ... Read More

Extract the value of the to a variable using JavaScript?

AmitDiwan
Updated on 01-Sep-2020 11:22:53

740 Views

To extract the value of the , use −document.getElementById(“yourTextIdValue”).textContentExample Live Demo Document This is the JavaScript....    var originalText = document.getElementById("myText").textContent;    console.log(originalText); 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 display the value −

Set JavaScript object values to null?

Disha Verma
Updated on 17-Mar-2025 12:35:38

6K+ Views

Setting object values to null can help reset data, free up memory, or show that a value is no longer needed. In JavaScript, the null value is used to indicate that there is an intentional absence of any object value. This article provides various methods for setting JavaScript object values to null and what that means. Setting an Object's values to null Setting object values to null can be done in two types: For Specific Object Value For Multiple Object Value For Specific Object Value Setting the specific ... Read More

Check if string begins with punctuation in JavaScript

AmitDiwan
Updated on 01-Sep-2020 11:14:53

713 Views

Let’s say we have the following strings. One of them is beginning with a question mark i.e. punctuation −var sentence1 = 'My Name is John Smith.' var sentence2 = '? My Name is John Smith.'We need to check whether any of the above two sentences begin with punctuation. To check if string begins with punctuation, the code is as follows −Examplevar punctuationDetailsRegularExpression=/^[., :!?]/ var sentence1 = 'My Name is John Smith.' var output1 = !!sentence1.match(punctuationDetailsRegularExpression) if(output1==true)    console.log("This ("+sentence1+") starts with a punctuation"); else    console.log("This ("+sentence1+") does not starts with a punctuation"); var sentence2 = '? My Name is ... Read More

Advertisements