Best Way to Reduce and Merge a Collection of Objects in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:17:19

671 Views

The best way to reduce and merge a collection of objects, use the concept of Object.values() along with reduce().Following is the object −var details = [    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 11, marks: 98, studentName: "Bob" } ];ExampleFollowing is the code to reduce and merge −var details = [    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 10, marks: 75, studentName: "John" },    { studentId: 11, marks: 98, studentName: "Bob" } ]; output = Object.values(details.reduce((value, object) => {   ... Read More

Difference Between Two Times Using Day.js JavaScript Library

AmitDiwan
Updated on 03-Oct-2020 15:15:09

481 Views

Let’s say the following are our time data −var startHour = dayjs().hour(10) var endHour = dayjs().hour(22)To get the difference, use the diff() method −ExampleFollowing is the code − Live Demo            Document    var startHour = dayjs().hour(10)    var endHour = dayjs().hour(22)    console.log("The hours difference is=" + endHour.diff(startHour, "hours")); To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output is as follows −

Checking a Checkbox with JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:13:12

554 Views

Let’s say the following are ourrinput type checkbox −John David We want to check any of the checkbox. Use the checked property to check the checkbox.ExampleFollowing is the code − Live Demo            Document    John        David        document.querySelector('#checkedValue2').checked = true To run the above program, save the file name anyName.html(index.html). Right click on the file and select the option “Open with live server” in VS Code editor −OutputThe output is as follows −

Replace Preceding Characters with Asterisks in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:11:14

891 Views

Let’s say the following are our values −'6778922' '76633 56 1443' '8888 4532 3232 9999'We want the preceding characters to be replaced with 4 asterisks and the display rest of the last 3 characters. The output should be −**** 922 **** 443 **** 999For such conditions, use the replace() and set regex in it.ExampleFollowing is the code −const hideDataWithDot = value => value.replace(/.+(.{3})$/, "**** $1"); console.log(hideDataWithDot('6778922')) console.log(hideDataWithDot('76633 56 1443')) console.log(hideDataWithDot('8888 4532 3232 9999')) To run the above program, use the following command −node fileName.js. Here, my file name is demo236.js.OutputThe output is as follows −PS C:\Users\Amit\javascript-code> node demo236.js **** ... Read More

Split Sentences by Comma and Remove Surrounding Spaces in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:09:39

8K+ Views

Let’s say the following is our string with comma and whitespace −var sentences = "  John ,    David ,         Bob ,      Mike,            Carol        ";To split the sentences by comma, use split(). For removing surrounding spaces, use trim().ExampleFollowing is the code −var sentences = "  John , David , Bob , Mike, Carol "; console.log("The value=" + sentences); var result = sentences.split(", ").map(function (value) {    return value.trim(); }); console.log("After modifying the value=") console.log(result);To run the above program, use the following command −node fileName.js.Here, my file ... Read More

Make an Array with Unique Elements in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:07:15

212 Views

Let’s say the following is our array with duplicate elements −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000];We want the output to be −[10, 20, 100, 40, 1000];To display only the unique elements, use the concept of filter.ExampleFollowing is the code −var duplicateNumbers = [10, 20, 100, 40, 20, 10, 100, 1000]; console.log("With Duplicates Values="); console.log(duplicateNumbers); var noDuplicateNumbersArray = duplicateNumbers.filter(function (value, index, array) {     return array.indexOf(value) === index; } ); console.log("Without Duplicates Values=") console.log(noDuplicateNumbersArray);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo234.js.OutputThe output is as ... Read More

Compare and Swap Adjacent Elements in JavaScript

AmitDiwan
Updated on 03-Oct-2020 15:05:38

383 Views

This is the concept of Bubble Sort. It compares to adjacent element if it is lesser it will swap the value.ExampleFollowing is the code −var numbers = [10, 100, 30, 40, 90, 4, 91, 56, 78]; function bubbleSorting(numbers) {    for (var outer = 0; outer < numbers.length; outer++) {       for (var inner = 0; inner < numbers.length; inner++) {          if (numbers[outer] < numbers[inner]) {             var temp = numbers[outer];             numbers[outer] = numbers[inner];             numbers[inner] = temp; ... Read More

Match the Style of Pre Element While Performing Paste in jQuery

AmitDiwan
Updated on 03-Oct-2020 15:02:13

115 Views

For this, use tag. Set it to contenteditable −We have set the following style for paste −    pre {       min-height: 150px;       min-width: 300px;       font-family: 'Times New Roman', Times, serif;       white-space: pre;       background-color: rgb(19, 22, 27);       color: #98d8e7;    } Now, you can use paste event listener −var getTheData = document.getElementById('data'); getTheData.addEventListener('paste', PutTheDataOnEditor);ExampleFollowing is the code − Live Demo            Document pre {    min-height: 150px;    min-width: 300px;    font-family: ... Read More

Get Value of Data Attribute in jQuery

AmitDiwan
Updated on 03-Oct-2020 14:57:06

8K+ Views

To get value of data attribute, use −$(“yourSelector”).data()The following is our input type with data attribute −ExampleFollowing is the code − Live Demo            Document        var value = $('#txtValue').data('value');    console.log("The value is=" + value); OutputThe output is as follows −

Prime Test in JavaScript by Creating a Custom Function

AmitDiwan
Updated on 03-Oct-2020 14:52:27

169 Views

Create a custom function to test a number is prime or not in JavaScript.ExampleFollowing is the code −function checkNumberIsPrime(number) {    var flag = false;    for (start = 2; start < number / 2; start++) {       if (number % start === 0) {          flag = false;             break;       }       else {          flag = true;       }    }    return flag; } var number = 11; if (checkNumberIsPrime(number) == true) {    console.log("The number is prime"); } else {    console.log("The number is not prime"); }To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo230.js.OutputThe output is as follows −PS C:\Users\Amit\JavaScript-code> node demo230.js The number is prime

Advertisements