AmitDiwan has Published 10744 Articles

Get the first element of array in JavaScript

AmitDiwan

AmitDiwan

Updated on 14-Sep-2020 06:42:43

990 Views

You can use simple for loop along with some if else condition to get the array’s first element in JavaScript.The logic is that, first of all check the array length is greater than 1 or not if the length is not equal to 1 that means there is no element ... Read More

Concatenate two arrays of objects and remove repeated data from an attribute in JavaScript?

AmitDiwan

AmitDiwan

Updated on 14-Sep-2020 06:39:13

242 Views

For this, use map() along with find(). Following is the code −Examplevar details1 = [    {       productDetails:       {          isSold: true,          productId:101       }    },    {       productDetails:     ... Read More

How can I declare and define multiple variables in one statement with JavaScript?

AmitDiwan

AmitDiwan

Updated on 14-Sep-2020 06:36:57

291 Views

To declare and define multiple variables in one statement, you can use below syntax −var anyVariableName1=yourValue1, anyVariableName2=yourValue2, anyVariableName3=yourValue3, anyVariableName4=yourValue4, . . NExamplevar firstName="My First Name is David", lastName="My Last Name is Miller", place="I live in AUS"; console.log(firstName); console.log(lastName); console.log(place);To run the above program, you need to use the following command ... Read More

How do I check whether a checkbox is checked in jQuery?

AmitDiwan

AmitDiwan

Updated on 14-Sep-2020 06:34:44

327 Views

To check whether a checkbox is checked in jQuery, use the concept of toggle(). Following is the code −Example Live Demo Document Your name is Adam Smith    $('#selectName').click(function() {       $("#txtName").toggle(this.checked);    }); To ... Read More

Display in console if Select list value contains a value from an array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 14-Sep-2020 06:30:59

659 Views

Let’s say the following is our dropdown −    John    David    Chris    Mike    Bob    Carol Following is our array −var listOfNames = ["Chris", "Robert", "Mike"];To check whether the selected list value contains a value in array, use −$(‘select’).on(‘change’).Example Live Demo Document ... Read More

From a list of IDs with empty and non-empty values, retrieve specific ID records in JavaScript

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 09:01:44

227 Views

Let’s say the following is our list −var details=[    {id:101, name:"John", age:21},    {id:111, name:"David", age:24},    {id:1, name:"Mike", age:22},    {id:"", name:"Sam", age:20},    {id: 1, name:"Carol", age:23},    {id:null, name:"Robert", age:25},    {id:1, name:"Adam", age:24},    {id:"", name:"Chris", age:23} ];You can use the concept of filter to ... Read More

Convert a list of string coords into two float lists of Lat/Longitude coordinates in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 09:00:14

593 Views

Let’s say the following are our coordinates −var listOfStrings = ["10.45322, -6.8766363", "78.93664664, -9.74646646", "7888.7664664, -10.64664632"];To convert the above into two float lists of Latitude and Longitude, use split() on the basis of comma(, ) along with map().Examplevar listOfStrings = ["10.45322, -6.8766363", "78.93664664, -9.74646646", "7888.7664664, -10.64664632"]; var latitude = []; ... Read More

Fetch values by ignoring a specific one in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:57:41

375 Views

To ignore a specific value, use the logical Not (!) operator in if condition and fetch watching you want to include.Examplevar customerDetails=[    {       customerName:"John",       customerAge:28,       customerCountryName:"US"    },    {       customerName:"David",       customerAge:25,     ... Read More

Select random values from an array in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:56:31

537 Views

To select random values from an array, use the concept of Math.random().Examplevar subjectNames = ["Javascript", "MySQL", "Java", "MongoDB", "Python", "Spring Framework"]; for(var index = subjectNames.length - 1; index > 0; index--){    var rndIndex = Math.floor(Math.random() * (index + 1));    var subjNameTemp = subjectNames[rndIndex];    subjectNames[rndIndex] = subjectNames[index];   ... Read More

Get only specific values in an array of objects in JavaScript?

AmitDiwan

AmitDiwan

Updated on 12-Sep-2020 08:55:15

13K+ Views

Let’s say the following is our array of objects −var details = [{    studentName: "John",    studentMarks: 92 }, {    studentName: "David",    studentMarks: 89 }, {    studentName: "Mike",    studentMarks: 98 }, ];To get only specific values in an array of objects in JavaScript, use the ... Read More

Advertisements