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

Get Key Name When Value is Empty in JavaScript Object

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

859 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

Maximum Sum Subsequence with At Least K Distant Elements in C++

Ayush Gupta
Updated on 09-Sep-2020 13:30:59

198 Views

In this tutorial, we will be discussing a program to find maximum sum subsequence with at-least k distant elements.For this we will be provided with an array containing integers and a value K. Our task is to find the subsequence having maximum sum such that all the elements are at least K elements apart.Example Live Demo#include using namespace std; //finding maximum sum subsequence int maxSum(int arr[], int N, int k) {    int MS[N];    MS[N - 1] = arr[N - 1];    for (int i = N - 2; i >= 0; i--) {       if (i ... Read More

Maximum Sum Subarray with Same Start and End Values in C++

Ayush Gupta
Updated on 09-Sep-2020 13:29:00

218 Views

In this tutorial, we will be discussing a program to find maximum sum subarray such that start and end values are same.For this we will be provided with an array containing integers. Our task is to find the subarray with the maximum sum such that the elements are both its ends are equal.Example Live Demo#include using namespace std; //finding the maximum sum int maxValue(int a[], int n) {    unordered_map first, last;    int pr[n];    pr[0] = a[0];    for (int i = 1; i < n; i++) {       pr[i] = pr[i - 1] + a[i]; ... Read 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

Maximum Sum Rectangle in a 2D Matrix - DP 27 in C++

Ayush Gupta
Updated on 09-Sep-2020 13:26:57

233 Views

In this tutorial, we will be discussing a program to find maximum sum rectangle in a 2D matrix.For this we will be provided with a matrix. Our task is to find out the submatrix with the maximum sum of its elements.Example Live Demo#include using namespace std; #define ROW 4 #define COL 5 //returning maximum sum recursively int kadane(int* arr, int* start, int* finish, int n) {    int sum = 0, maxSum = INT_MIN, i;    *finish = -1;    int local_start = 0;    for (i = 0; i < n; ++i) {       sum += arr[i];   ... Read More

Maximum Sum Possible for a Sub-sequence in C++

Ayush Gupta
Updated on 09-Sep-2020 13:24:28

143 Views

In this tutorial, we will be discussing a program to find maximum sum possible for a sub-sequence such that no two elements appear at a distance < K in the array.For this we will be provided with an array containing N intergers and a value K. Our task is to find the maximum sum of the subsequence including elements not nearer than K.Example Live Demo#include using namespace std; //returning maximum sum int maxSum(int* arr, int k, int n) {    if (n == 0)       return 0;    if (n == 1)       return arr[0];   ... Read More

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

Maximum Sum Path in a Matrix from Top to Bottom in C++

Ayush Gupta
Updated on 09-Sep-2020 13:22:05

301 Views

In this tutorial, we will be discussing a program to find maximum sum path in a matrix from top to bottom.For this we will be provided with a matrix of N*N size. Our task is to find the maximum sum route from top row to bottom row while moving to diagonally higher cell.Example Live Demo#include using namespace std; #define SIZE 10 //finding maximum sum path int maxSum(int mat[SIZE][SIZE], int n) {    if (n == 1)       return mat[0][0];    int dp[n][n];    int maxSum = INT_MIN, max;    for (int j = 0; j < n; j++) ... Read More

Creating 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' ] }

Advertisements