AmitDiwan has Published 10740 Articles

Retrieve property value selectively from array of objects in JavaScript

AmitDiwan

AmitDiwan

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

334 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},   ... Read More

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

AmitDiwan

AmitDiwan

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

381 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 ... Read More

Join two objects by key in JavaScript

AmitDiwan

AmitDiwan

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

857 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: ... Read More

Form a sequence out of an array in JavaScript

AmitDiwan

AmitDiwan

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

831 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 ... Read More

Array of objects to array of arrays in JavaScript

AmitDiwan

AmitDiwan

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

907 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 ... Read More

Trim and split string to form array in JavaScript

AmitDiwan

AmitDiwan

Updated on 23-Nov-2020 10:24:18

891 Views

Suppose, we have a comma-separated string like this −const str = "a, b, c, d , e";We are required to write a JavaScript function that takes in one such and sheds it off all the whitespaces it contains.Then our function should split the string to form an array of literals ... Read More

Removing duplicates and keep one instance in JavaScript

AmitDiwan

AmitDiwan

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

255 Views

We are required to write a JavaScript function that takes in an array of literal values. The array might contain some repeating values inside it.Our function should remove all the repeating values keeping the first instance of repeating value in the array.ExampleThe code for this will be −const arr = ... Read More

JavaScript - Merge two arrays according to id property

AmitDiwan

AmitDiwan

Updated on 23-Nov-2020 10:21:46

5K+ Views

Suppose we have two arrays of objects, the first of which contains some objects with user ids and user names.The array contains objects with user ids and user addresses.The array is −const arr1 = [    {"id":"123", "name":"name 1"},    {"id":"456", "name":"name 2"} ]; const arr2 = [    {"id":"123", ... Read More

Elements that appear twice in array in JavaScript

AmitDiwan

AmitDiwan

Updated on 23-Nov-2020 10:19:44

591 Views

We are required to write a JavaScript function that takes in an array of literal values. Our function should pick all those values from the array that appears exactly twice in the array and return a new array of those elements.ExampleThe code for this will be −const arr = [0, ... Read More

How to merge two different array of objects using JavaScript?

AmitDiwan

AmitDiwan

Updated on 23-Nov-2020 10:16:52

556 Views

Suppose, we have two different array of objects that contains information about the questions answered by some people −const arr1=[    { PersonalID: '11', qusetionNumber: '1', value: 'Something' },    { PersonalID: '12', qusetionNumber: '2', value: 'whatever' },    { PersonalID: '13', qusetionNumber: '3', value: 'anything' },    { PersonalID: ... Read More

Advertisements