Found 6683 Articles for Javascript

How can I merge properties of two JavaScript objects dynamically?

AmitDiwan
Updated on 24-Oct-2020 12:23:58

168 Views

To merger properties of two objects, you can use the concept of {...object1,...object2,... N).ExampleFollowing is the code −var firstObject = {    firstName: 'John',    lastName: 'Smith' }; var secondObject = {    countryName: 'US' }; var mergeObject = {...firstObject, ...secondObject}; console.log(mergeObject);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo307.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo307.js { firstName: 'John', lastName: 'Smith', countryName: 'US' }

How to get all combinations of some arrays in JavaScript?

AmitDiwan
Updated on 24-Oct-2020 12:22:27

268 Views

You can use your own function to get all combinations.ExampleFollowing is the code −function combination(values) {    function * combinationRepeat(size, v) {       if (size)          for (var chr of values)       yield * combinationRepeat(size - 1, v + chr);       else yield v;    }    return [...combinationRepeat(values.length, "")]; } var output = combination([4,5]); console.log(output);To run the above program, you need to use the following command −node fileName.js.Here, my file name is demo306.js.OutputThis will produce the following output −PS C:\Users\Amit\javascript-code> node demo306.js [ '44', '45', '54', '55' ]

JavaScript/ Typescript object null check?

Nikhilesh Aleti
Updated on 18-Jun-2024 17:36:07

11K+ Views

In this article we will check if an object is a null in Typescript. A variable is undefined until and unless it is not assigned to any value after declaring it. NULL is known as empty or dosen’t exist. In typescript, unassigned values are by default undefined, so in order to make a variable null, we must assign it a null value. To check a variable is null or not in typescript we can use typeof or "===" operator. Using typeofoperator The typeof operator in JavaScript is used to find the datatype of the variable. Example In the example ... Read More

Recursion example in JavaScript to display numbers is descending order?

Nikhilesh Aleti
Updated on 08-Nov-2022 08:20:18

783 Views

In this article, the given task is to display the numbers in descending order by using recursion. Let’s get an understanding of the task by looking into the input-output scenarios. Input-Output scenario Let’s look into an input-output scenario, where there is an input number and we need to print the numbers in descending order from the given number to 1. Input = 10; Output = 10 9 8 7 6 5 4 3 2 1 What is recursion? The process of calling itself is called recursion. The function which will call itself is known as a recursive function. Syntax Following ... Read More

How to replace elements in array with elements of another array in JavaScript?

Nikhilesh Aleti
Updated on 22-Sep-2022 12:22:02

6K+ Views

The given task is to replace the elements in an array with elements of another array. Input-Output Scenario Let’s look into some input-output scenarios. Consider there are there two arrays with elements in it. The first array is having more elements than the second array and we are replacing a part of elements in the first array with the elements from second array. Array1 = [1, 3, 5, 7, 2, 4]; Array2 = [3, 6]; Output = [3, 6, 5, 7, 2, 4] // elements got replaced Let’s consider another scenario, where the elements in the first array are ... Read More

Comparing and filling arrays in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:15:24

77 Views

We are required to write a function that compares two arrays and creates a third array filling that array with all the elements of the second array and filling null for all those elements that are present in the first array but misses out in the second array.For example:If the two arrays are −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h'];Then the output should be −const output = ['f', null, 'h'];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = ['f', 'g', 'h']; const arr2 = ['f', 'h']; const compareAndFill ... Read More

Join arrays to form string in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:16:03

252 Views

The given task is to perform joining arrays to form strings in JavaScript. Input-Output Scenario Let’s look into some input-output scenarios. Consider there is an array having some elements in it and we are trying to join that array to form the string. Input = [32, 45, 65, 12, 07, 55]; Output = 32, 45, 65, 12, 07, 55 //String Let’s look into another scenario, where we are having two arrays and we are trying to join those two arrays and form a string. Array1 = [123, 453, 656, 654, 125, 757]; Array2 = ["Hello", "honey", "bunny"]; Output = ... Read More

Removing Negatives from Array in JavaScript

Nikhilesh Aleti
Updated on 22-Sep-2022 12:18:22

1K+ Views

The given task is to remove the negative values from the array. Input-Output Scenario Let’s look into input output scenarios. Consider there is an array and it is having both negative and positive integer values. We need to remove the negative values from the array and return the array. Input = [-2, 5, -7, 32, 78, -32]; Output = [5, 32, 78] Now let’s assume there are no negatives integer values in the array. so, the array will be returned in the same order. Input = [56, 43, 12, 67, 69, 34]; Output = [56, 43, 12, 67, 69, ... Read More

Fetching odd appearance number in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:10:45

58 Views

Given an array of integers, we are required to write a function that takes this array and finds the one element that appears an odd number of times.There will always be only one integer that appears an odd number of times. We will approach this problem by sorting the array. Once sorted, we can iterate over the array to pick the element that appears for odd number of times.ExampleThe code for this will be −const arr = [20, 1, -1, 2, -2, 3, 3, 5, 5, 1, 2, 4, 20, 4, -1, -2, 5]; const findOdd = arr => { ... Read More

Greatest number in a dynamically typed array in JavaScript

AmitDiwan
Updated on 24-Oct-2020 12:08:49

89 Views

We are required to write a JavaScript function that takes in an array that contains some numbers, some strings and some false values. Our function should return the biggest Number from the array.For example: If the input array is −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii'];Then the output should be 65.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [23, 'hello', undefined, null, 21, 65, NaN, 1, undefined, 'hii']; const pickBiggest = arr => {    let max = -Infinity;    for(let i = 0; i ... Read More

Advertisements