Found 10483 Articles for Web Development

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

How to get key name when the value contains empty in an object with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:32:44

838 Views

Let’s say the following is our object −var details = {    firstName: 'John',    lastName: '',    countryName: 'US' }Use Object.keys() along with find() to get the key name with empty value. Following is the code −Examplevar details = {    firstName: 'John',    lastName: '',    countryName: 'US' } var result = Object.keys(details).find(key=> (details[key] === '' || details[key] === null)); console.log("The key is="+result);To run the above program, you need to use the following command −node fileName.js.OutputHere, my file name is demo118.js. This will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo118.js The key is=lastNameRead More

Check whether a value exists in JSON object?

AmitDiwan
Updated on 09-Sep-2020 13:28:22

5K+ Views

Let’s say the following is our object −var apiJSONObject = [    {subjectName:"MySQL"},    {subjectName:"Java"},    {subjectName:"JavaScript"},    {subjectName:"MongoDB"} ]Let’s check for existence of a value “JavaScript” −Examplevar apiJSONObject = [    {subjectName:"MySQL"},    {subjectName:"Java"},    {subjectName:"JavaScript"},    {subjectName:"MongoDB"} ] for(var i=0;i node demo117.js The search found in JSON Object

HTML form action and onsubmit validations with JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:23:41

3K+ Views

Let’s see an example wherein we are validating input text onsubmit −Example Live Demo Document    function validateTheForm(){       var validation = (document.getElementById('txtInput').value == 'gmail');       if(!validation){          alert('Something went wrong...Plese write gmail intext box and click');          return false;       }       return true;    } 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” ... Read More

Creating an associative array in JavaScript with push()?

AmitDiwan
Updated on 09-Sep-2020 13:20:18

2K+ Views

For this, use forEach() loop along with push(). Following is the code −Examplevar studentDetails= [    {       studentId:1,       studentName:"John"    },    {       studentId:1,       studentName:"David"    },    {       studentId:2,       studentName:"Bob"    },    {       studentId:2,       studentName:"Carol"    } ] studentObject={}; studentDetails.forEach (function (obj){    studentObject[obj.studentId] = studentObject[obj.studentId] || [];    studentObject[obj.studentId].push(obj.studentName); }); console.log(studentObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo116.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo116.js { '1': [ 'John', 'David' ], '2': [ 'Bob', 'Carol' ] }

Creating an associative array in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:17:38

1K+ Views

You can create an associative array in JavaScript using an array of objects with key and value pair.Associative arrays are basically objects in JavaScript where indexes are replaced by user defined keys.Examplevar customerDetails= [    {       "customerId":"customer-1",       "customerName":"David",       "customerCountryName":"US"    },    {       "customerId":"customer-2",       "customerName":"Bob",       "customerCountryName":"UK"    },    {       "customerId":"customer-3",       "customerName":"Carol",       "customerCountryName":"AUS"    } ] for(var i=0;i node demo115.js David Bob Carol

How to display JavaScript array items one at a time in reverse on button click?

AmitDiwan
Updated on 09-Sep-2020 13:16:02

786 Views

Let’s say the following is our array −var listOfNames = [    'John',    'David',    'Bob' ];Following is our button −Click The Button To get the Reverse ValueNow, for array items in reverse, at first, reach the array length and decrement the length by 1. Then, print the contents of the particular index in reverse order.Example Live Demo Document Click The Button To get the Reverse Value    var listOfNames = [       'John',       'David',       'Bob'    ];    count=listOfNames.length-1;    function reverseTheArray(){       document.getElementById('reverseTheArray').innerHTML =       listOfNames[count];       count--;       if (count

Assign multiple variables to the same value in JavaScript?

AmitDiwan
Updated on 09-Sep-2020 13:10:44

4K+ Views

To assign multiple variables to the same value, the syntax is as followsvar anyVariableName1, anyVariableName2, anyVariableName3……….N; yourVariableName1=yourVariableName2=yourVariableName3=.........N=yourValue;Let’s say the following are our variables and we are assigning same value −var first, second, third, fourth, fifth; first=second=third=fourth=fifth=100;Examplevar first, second, third, fourth, fifth; first=second=third=fourth=fifth=100; console.log(first); console.log(second); console.log(third); console.log(fourth); console.log(fifth); console.log("The sum of all values="+(first+second+third+fourth+fifth));To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo114.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo114.js 100 100 100 100 100 The sum of all values=500Read More

Creating a JavaScript Object from Single Array and Defining the Key Value?

AmitDiwan
Updated on 09-Sep-2020 13:09:40

152 Views

To convert a JavaScript object to key value, you need to use Object.entries() along with map(). Following is the code −Examplevar studentObject={    101: "John",    102: "David",    103: "Bob" } var studentDetails = Object.assign({}, studentObject) studentDetails = Object.entries(studentObject).map(([studentId,studentName])=>({studentId ,studentName})); console.log(studentDetails);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo113.js.OutputThis will produce the following output −PS C:\Users\Amit\JavaScript-code> node demo113.js [    { studentId: '101', studentName: 'John' },    { studentId: '102', studentName: 'David' },    { studentId: '103', studentName: 'Bob' } ]

Advertisements