Found 6710 Articles for Javascript

JavaScript Adding array name property to JSON object [ES5] and display in Console?

AmitDiwan
Updated on 10-Sep-2020 07:11:11

163 Views

Following is our object −var customerDetails ={    "customerFirstName":"David",    "customerLastName":"Miller",    "customerAge":21,    "customerCountryName":"US" };Now, create a new array and use push() function. Following is the code −Examplevar customerDetails ={    "customerFirstName":"David",    "customerLastName":"Miller",    "customerAge":21,    "customerCountryName":"US" }; var customerObjectToArray = []; customerObjectToArray.push(customerDetails); console.log(customerObjectToArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo125.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo125.js [    {       customerFirstName: 'David',       customerLastName: 'Miller',       customerAge: 21,       customerCountryName: 'US'    } ]

Insert a word before the file name’s dot extension in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:07:14

449 Views

Let’s say the following is our file name −var actualJavaScriptFileName = "demo.js";Following is the word to be inserted before the dot extension −var addValueBetweenFileNameAndExtensions = "programming";At first, you need to split() the file name on the basis of dot(.) and then to insert a character, you can use the concept of template variables. Following is the code −Examplevar addValueBetweenFileNameAndExtensions = "programming"; var actualJavaScriptFileName = "demo.js"; console.log("The actual File name="+actualJavaScriptFileName); var [fileName, fileExtension] = actualJavaScriptFileName.split('.'); console.log("After adding into the file name="); console.log(`${fileName}- ${addValueBetweenFileNameAndExtensions}.${fileExtension}`)To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo124.js. ... Read More

How to convert string type value to array type in JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:06:03

401 Views

To convert string type value to array type, use the parse() method. Following is the code −Examplevar customerDetails='[    {"name": "John", "countryName": "US"},    {"name": "David", "countryName": "AUS"},    {"name": "Bob", "countryName": "UK"} ]'; console.log("The actual value="+customerDetails); var convertStringToArray=JSON.parse(customerDetails); console.log("After converting string to array objects="); console.log(convertStringToArray);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo123.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo123.js The actual value=[{"name": "John", "countryName": "US"}, {"name": "David", "countryName": "AUS"}, {"name": "Bob", "countryName": "UK"}] After converting string to array objects=[    { name: 'John', countryName: 'US' ... Read More

How to set text in tag with JavaScript?

AmitDiwan
Updated on 10-Sep-2020 07:04:29

1K+ Views

At first, set the element −Replace This strong tagThe id attribute set above would be used set the text using # −$(document).ready(function(){    $("#strongDemo").html("Actual value of 5+10 is 15....."); });Example Live Demo Document Replace This strong tag    $(document).ready(function(){       $("#strongDemo").html("Actual value of 5+10 is 15.....");    }); 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 get row count of an HTML table

Alshifa Hasnain
Updated on 06-Dec-2024 21:39:08

5K+ Views

Tables are a common way to organize and display data in HTML. If you're working with tables dynamically in JavaScript, you might need to count the rows in an HTML table for various purposes, such as validation, manipulation, or displaying statistics. In this article, we will demonstrate how to retrieve the row count of an HTML table using JavaScript. We will use the rows Attribute to access the row count through rows.length, while in the other method, we will specifically count the rows through tbody selection. What is rows Attribute? The rows attribute in HTML is used to define the ... Read More

How to group JSON data in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 14:11:54

3K+ Views

To group JSON data, you need to extract all the keys and use the push(). Following is the code −Examplevar details= {    "1":    {       name:"John"    },    "2":    {       name:"John"    },    "3":    {       name:"David"    }    var objectWithGroupByName = {};    for (var key in details){       var name = details[key].name;    if (!objectWithGroupByName[name]){       objectWithGroupByName[name] = [];    }    objectWithGroupByName[name].push(details[key]); } console.log(objectWithGroupByName);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo122.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo122.js {    John: [ { name: 'John' }, { name: 'John' } ],    David: [ { name: 'David' } ] }

Call a JavaScript class object with variable?

AmitDiwan
Updated on 09-Sep-2020 14:09:58

1K+ Views

Let’s say the following is our variable −var message = 'This is the Class Demo';Following is our object, var object = new FirstClass(message)The class FirstClass −class FirstClass{    constructor( message){       this.message = message;    } }We will use eval() to call a JavaScript class object with variable. Following is the code −Exampleclass FirstClass{    constructor( message){       this.message = message;    } } var message = 'This is the Class Demo'; var object = new FirstClass(message) console.log(eval(object).message);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo121.js.OutputThis ... Read More

JavaScript Submit textbox on pressing ENTER?

Alshifa Hasnain
Updated on 23-Dec-2024 18:07:02

3K+ Views

In this article, we will learn to Submit a textbox by pressing ENTER in JavaScript. Submitting a textbox form by pressing the ENTER key is a common requirement in web development, enhancing the user experience by making interactions smoother and more intuitive. Why Enable Form Submission on ENTER Key Press? Allowing users to submit a form by pressing the ENTER key is an efficient way to enhance usability and reduce friction in the user interface. Whether you're building a search bar, login form, or any other type of text input, implementing this feature saves time and makes interactions feel ... Read More

How to convert comma separated text in div into separate lines with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:52:21

3K+ Views

Let’s say the following is our comma separated text in div − This, is, the, first, JavaScript, program To convert comma separated text into separate lines, you need to use trim() along with split() on the basis of comma(, ).Example Live Demo Document This, is, the, first, JavaScript, program    var allTheData = document.querySelector('.commaSeparated').textContent.trim().split(', ')    var separateList = ''    allTheData.forEach(function(value) {       separateList += '' + value + '';    });    separateList += '';    document.querySelector(".commaSeparated").innerHTML = separateList; To run the ... Read More

How to use JavaScript map() method to access nested objects?

AmitDiwan
Updated on 09-Sep-2020 13:36:30

5K+ Views

Let’s say the following is our nested objects −var details = [    {       id:"101",       firstName:"John",       lastName:"Smith",       age:25,       countryName:"US",       subjectDetails: {          subjectId:"Java-101",          subjectName:"Introduction to Java"       },    },    {       "uniqueId": "details_10001"    } ]Use map() along with typeOf to access nested objects. Following is the code −Examplevar details = [    {       id:"101",       firstName:"John",       lastName:"Smith",       ... Read More

Advertisements