Javascript Articles

Page 118 of 534

Search a complex object by id property in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 475 Views

Suppose we have a complex JSON Object like this − const obj = { "id": "0001", "fieldName": "sample1", "fieldValue": "0001", "subList": [ { "id": 1001, "fieldName": "Sample Child 1", "fieldValue": "1001", "subList": [] }, { "id": 1002, "fieldName": "Sample Child 2", ...

Read More

Return indexes of greatest values in an array in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 454 Views

We are required to write a JavaScript function that takes in an array of Numbers. The array may contain more than one greatest element (i.e., repeating greatest element). We are required to write a JavaScript function that takes in one such array and returns all the indices of the greatest element. Using Array.reduce() Method This approach first finds the maximum value using Math.max(), then uses reduce() to collect all indices where the element equals the maximum value. const arr = [10, 5, 4, 10, 5, 10, 6]; const findGreatestIndices = arr => { ...

Read More

Use array as sort order in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 198 Views

In JavaScript, you can sort an array of objects based on a custom order defined by another array. This is useful when you need to arrange data according to a specific sequence rather than alphabetical or numerical order. Problem Statement Given a reference array that defines the desired order and an array of objects, we need to sort the objects so their property values match the sequence in the reference array. const sort = ["this", "is", "my", "custom", "order"]; const myObjects = [ {"id":1, "content":"is"}, {"id":2, "content":"my"}, ...

Read More

Checking if a key exists in a JavaScript object

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 520 Views

We are required to illustrate the correct way to check whether a particular key exists in an object or not. Before moving on to the correct way let's first examine an incorrect way and see how it's actually incorrect. Way 1: Checking for undefined value (incorrect way) Due to the volatile nature of JavaScript, we might want to check for the existence of key in an object like this: const obj = { name: 'Rahul' }; // Incorrect approaches console.log(!obj['fName']); ...

Read More

Extract arrays separately from array of Objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 1K+ Views

Suppose, we have an array of objects like this − const arr = [{ name : 'Client 1', total: 900, value: 12000 }, { name : 'Client 2', total: 10, value: 800 }, { name : 'Client 3', total: 5, value : 0 }]; We are required to write a JavaScript function that takes in one such array and extracts a separate array for each object property. Therefore, ...

Read More

Filter an array containing objects based on another array containing objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 13K+ Views

Suppose we have two arrays of objects like these − const arr1 = [{id:'1', name:'A'}, {id:'2', name:'B'}, {id:'3', name:'C'}, {id:'4', name:'D'}]; const arr2 = [{id:'1', name:'A', state:'healthy'}, {id:'3', name:'C', state:'healthy'}]; We are required to write a JavaScript function that takes in two such arrays. Our function should return a new filtered version of the first array (arr1 in this case) that contains only those objects with a name property that are not contained in the second array (arr2 in this case) with the same name property. Therefore, the output, in this case, should look like ...

Read More

Iterate through Object keys and manipulate the key values in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 310 Views

When working with arrays of objects in JavaScript, you often need to iterate through object keys and transform their values. This article demonstrates how to extract specific elements from array values within objects. Problem Statement Suppose we have an array of objects where each property contains an array of values: const arr = [ { col1: ["a", "b"], col2: ["c", "d"] }, { ...

Read More

Checking for straight lines in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 545 Views

We need to write a JavaScript function that takes an array of coordinate pairs and determines if all points form a straight line. Each subarray contains exactly two items representing x and y coordinates. Our function checks whether the coordinates form a straight line by calculating and comparing slopes between points. For example: [[4, 5], [5, 6]] should return true (slope = 1) [[1, 2], [2, 3], [4, 5]] should return true (all have slope = 1) [[1, 1], [2, 2], [3, 4]] should return false (different slopes) The array is guaranteed to contain ...

Read More

Finding the power of a string from a string with repeated letters in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 581 Views

The power of a string is the maximum length of a non-empty substring that contains only one unique character. We are required to write a JavaScript function that takes in a string and returns its power. For example − const str = "abbcccddddeeeeedcba" Then the output should be 5, because the substring "eeeee" is of length 5 with the character 'e' only. How It Works The algorithm traverses the string and counts consecutive identical characters. It tracks the maximum count found, which represents the power of the string. ...

Read More

Finding day of week from date (day, month, year) in JavaScript

AmitDiwan
AmitDiwan
Updated on 15-Mar-2026 791 Views

We are required to write a JavaScript function that takes in three arguments, namely: day, month and year. Based on these three inputs, our function should find the day of the week on that date. For example: If the inputs are − day = 15, month = 8, year = 1993 Output Then the output should be − const output = 'Sunday' Using Built-in Date Object (Simple Method) The easiest approach is to use JavaScript's built-in Date object: function getDayOfWeek(day, month, year) { ...

Read More
Showing 1171–1180 of 5,340 articles
« Prev 1 116 117 118 119 120 534 Next »
Advertisements