Found 6710 Articles for Javascript

JavaScript - array undefined element count

Alshifa Hasnain
Updated on 14-Jan-2025 03:27:25

836 Views

In this article, we will learn to count the number of undefined elements in an array using JavaScript. When working with arrays in JavaScript, you may encounter situations where certain elements are either explicitly set as undefined or are missing altogether (often represented by empty slots in sparse arrays). Problem Statement Given an array that may contain undefined values, we need to count how many elements in the array are defined (i.e., not undefined).  Input const arr = [12, undefined, "blabla", , true, 44]; Output 44 elements are defined (12, "blabla", true, and 44). Thus, the expected output is ... Read More

JavaScript: create an array of JSON objects from linking two arrays

AmitDiwan
Updated on 23-Nov-2020 10:43:43

473 Views

Suppose, we have two arrays like these −const meals = ["breakfast", "lunch", "dinner"]; const ingredients = [    ["eggs", "yogurt", "toast"],    ["falafel", "mushrooms", "fries"],    ["pasta", "cheese"] ];We are required to write a JavaScript function that takes in two such arrays and maps the subarrays in the second array to the corresponding strings of the first array.Therefore, the output for the above arrays should look like −const output = {    "breakfast" : ["eggs", "yogurt", "toast"],    "lunch": ["falafel", "mushrooms", "fries"],    "dinner": ["pasta", "cheese"] };ExampleThe code for this will be −const meals = ["breakfast", "lunch", "dinner"]; const ingredients ... Read More

Combining two arrays in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:42:21

250 Views

We are required to write a JavaScript function that takes in two arrays of the same length.Our function should then combine corresponding elements of the arrays, to form the corresponding subarray of the output array, and then finally return the output array.If the two arrays are −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3];Then the output should be −const output = [    ['a', 1],    ['b', 2],    ['c', 3] ];ExampleThe code for this will be −const arr1 = ['a', 'b', 'c']; const arr2 = [1, 2, 3]; const combineCorresponding = (arr1 = [], arr2 ... Read More

Remove item from a nested array by indices in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:41:00

1K+ Views

Suppose, we have a nested array of objects like this −const arr = [    { value: 'some value' },    {       array: [          { value: 'some value' },          {             array: [                { value: 'some value' },                { value: 'some value' },             ],          },          { value: 'some value' },          {   ... Read More

Join in nested array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:35:41

493 Views

Suppose, we have a nested array like this −const arr = ['zero', ['one', 'two' , 'three', ['four', ['five', 'six', ['seven']]]]];We are required to write a JavaScript function that takes in a nested array. Our function should then return a string that contains all the array elements joined by a semicolon (';')Therefore, for the above array, the output should look like −const output = 'zero;one;two;three;four;five;six;seven;';ExampleThe code for this will be −const arr = ['zero', ['one', 'two' , 'three', ['four', ['five', 'six', ['seven']]]]]; const buildString = (arr = [], res = '') => {    for(let i = 0; i < arr.length; ... Read More

Retrieve property value selectively from array of objects in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:34:29

312 Views

Suppose, we have an array of objects like this −const arr = [    { id : "23", name : "Item 1", isActive : true},    { id : "25", name : "Item 2", isActive : false},    { id : "26", name : "Item 3", isActive : false},    { id : "30", name : "Item 4", isActive : true},    { id : "45", name : "Item 5", isActive : true} ];We are required to write a JavaScript function that takes in one such object and return an array of the value of "id" property of all those ... Read More

Sort an array and place a particular element as default value in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:32:58

343 Views

We are required to write a JavaScript function that takes in an array of literal values as the first argument and a string as the second argument.Our function should sort the array alphabetically but keeping the string provided as the second argument (if it exists in the array) as the first element, irrespective of the text it contains.ExampleThe code for this will be −const arr = ["Apple", "Orange", "Grapes", "Pineapple", "None", "Dates"]; const sortKeepingConstants = (arr = [], text = '') => {    const sorter = (a, b) => {       return (b === text) - (a ... Read More

Join two objects by key in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:31:41

825 Views

Suppose, we have two child and parent JSON arrays of objects like these −const child = [{    id: 1,    name: 'somename',    parent: {       id: 2    }, }, {    id: 2,    name: 'some child name',    parent: {       id: 4    } }]; const parent = [{    id: 1,    parentName: 'The first',    child: {} }, {    id: 2,    parentName: 'The second',    child: {} }, {    id: 3,    parentName: 'The third',    child: {} }, {    id: 4,    parentName: 'The ... Read More

Form a sequence out of an array in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:28:25

809 Views

Suppose we have a sorted array of numbers like this where we can have consecutive numbers.const arr = [1, 2, 3, 5, 7, 8, 9, 11];We are required to write a JavaScript function that takes one such array.Our function should form a sequence for this array. The sequence should be such that for all the consecutive elements of the array, we have to just write the starting and ending numbers replacing the numbers in between with a dash (-), and keeping all other numbers unchanged.Therefore, for the above array, the output should look like −const output = '1-3, 5, 7-9, ... Read More

Array of objects to array of arrays in JavaScript

AmitDiwan
Updated on 23-Nov-2020 10:25:40

877 Views

Suppose, we have an array of objects like this −const arr = [    {"Date":"2014", "Amount1":90, "Amount2":800},    {"Date":"2015", "Amount1":110, "Amount2":300},    {"Date":"2016", "Amount1":3000, "Amount2":500} ];We are required to write a JavaScript function that takes in one such array and maps this array to another array that contains arrays instead of objects.Therefore, the final array should look like this −const output = [    ['2014', 90, 800],    ['2015', 110, 300],    ['2016', 3000, 500] ];ExampleThe code for this will be −const arr = [    {"Date":"2014", "Amount1":90, "Amount2":800},    {"Date":"2015", "Amount1":110, "Amount2":300},    {"Date":"2016", "Amount1":3000, "Amount2":500} ]; const arrify ... Read More

Advertisements