Front End Technology Articles

Page 394 of 652

Best way to flatten an object with array properties into one array JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 807 Views

Suppose, we have an object of arrays like this −const obj = {    arr_a: [9, 3, 2],    arr_b: [1, 5, 0],    arr_c: [7, 18] };We are required to write a JavaScript function that takes in one such object of arrays. The function should construct an flattened and merged array based on this object.Therefore, the final output array should look like this −const output = [9, 3, 2, 1, 5, 0, 7, 18];Exampleconst obj = {    arr_a: [9, 3, 2],    arr_b: [1, 5, 0],    arr_c: [7, 18] }; const objectToArray = (obj = {}) => {    const res = [];    for(key in obj){       const el = obj[key];       res.push(...el);    };    return res; }; console.log(objectToArray(obj));OutputAnd the output in the console will be −[    9, 3, 2, 1,    5, 0, 7, 18 ]

Read More

Construct objects from joining two strings JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 349 Views

We are required to write a JavaScript function that takes in two comma separated strings. The first string is the key string and the second string is the value string, the number of elements (commas) in both the strings will always be the same.Our function should construct an object based on the key and value strings and map the corresponding values to the keys.Exampleconst str1= '[atty_hourly_rate], [paralegal_hourly_rate], [advanced_deposit]'; const str2 = '250, 150, 500'; const mapStrings = (str1 = '', str2 = '') => {    const keys = str1.split(', ').map( (a) => {       return a.slice(1, -1); ...

Read More

Convert 2d tabular data entries into an array of objects in JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 212 Views

Suppose, we have an array of arrays like this −const arr = [    ["Ashley", "2017-01-10", 80],    ["Ashley", "2017-02-10", 75],    ["Ashley", "2017-03-10", 85],     ["Clara", "2017-01-10", 90],    ["Clara", "2017-02-10", 82] ];We are required to write a JavaScript function that takes in one such array as the first and the only input.The function should then construct a new array of objects based on the input array. The array should contain an object for each unique subarray of the input array. (by unique, in this context, we mean the subarray that have their first element unique).Each object must ...

Read More

Filtering array within a limit JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 775 Views

We are required to write a JavaScript function that takes in an array of numbers as the first argument and an upper limit and lower limit number as second and third argument respectively. Our function should filter the array and return a new array that contains elements between the range specified by the upper and lower limit (including the limits)Exampleconst array = [18, 23, 20, 17, 21, 18, 22, 19, 18, 20]; const lower = 18; const upper = 20; const filterByLimits = (arr = [], upper, lower) => {    let res = [];    res = arr.filter(el => {       return el >= lower && el

Read More

Map multiple properties in array of objects to the same array JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 1K+ Views

Suppose, we have an array of objects like this −const arr = [    {a: 1, b: 2},    {a: 3, b: 4},    {a: 5, b: 6} ];We are required to write a JavaScript function that takes in one such array of objects. The function should then map this array to an array of Number literals like this −const output = [1, 2, 3, 4, 5, 6];Exampleconst arr = [    {a: 1, b: 2},    {a: 3, b: 4},    {a: 5, b: 6} ]; const pushToArray = (arr = []) => {    const result = arr.reduce((acc, obj) => {     acc.push(obj.a);    acc.push(obj.b);    return acc; }, []);    return result; }; console.log(pushToArray(arr));OutputAnd the output in the console will be −[ 1, 2, 3, 4, 5, 6 ]

Read More

How to sort a JavaScript object list based on a property when the property is not consistent

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 144 Views

We have an array that contains various objects. A few objects on this array have a date field (which basically is returned as a string from the server, not a date object), while for others this field is null.The requirement is that we have to display objects without date at top, and those with date needs to be displayed after them sorted by date field.Also, for objects without date sorting needs to be done alphabetically.Exampleconst sorter = ((a, b) => {    if (typeof a.date == 'undefined' && typeof b.date != 'undefined') {       return -1;    } ...

Read More

Count number of entries in an object having specific values in multiple keys JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 332 Views

Suppose, we have an array of objects like this −const arr = [    {"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"},    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"},    {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ];We are required to write a JavaScript function that takes in one such array. The goal of function is to return an array of all such objects from the original array that have value "USA" for the "from" property of objects and value "INDIA" for the "to" property of objects.Exampleconst arr = [    {"goods":"Wheat ", "from":"GHANA", "to":"AUSTRALIA"},    {"goods":"Wheat", "from":"USA", "to":"INDIA"},    {"goods":"Wheat", "from":"SINGAPORE", "to":"MALAYSIA"},    {"goods":"Wheat", "from":"USA", "to":"INDIA"}, ...

Read More

Converting a comma separated string to separate arrays within an object JavaScript

AmitDiwan
AmitDiwan
Updated on 23-Nov-2020 651 Views

Suppose, we have a string like this −const str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james';We are required to write a JavaScript function that takes in one such string. The function should then prepare an object of arrays like this −const output = {    dress = ["cotton", "leather", "black", "red", "fabric"];    houses = ["restaurant", "school", "small", "big"];    person = ["james"]; };Exampleconst str = 'dress/cotton/black, dress/leather/red, dress/fabric, houses/restaurant/small, houses/school/big, person/james'; const buildObject = (str = '') => {    const result = {};    const strArr = str.split(', ');    strArr.forEach(el => {       const values ...

Read More

Flatten array to 1 line in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 273 Views

Suppose, we have a nested array of numbers like this −const arr = [    [ 0, 0, 0, −8.5, 28, 8.5 ],    [ 1, 1, −3, 0, 3, 12 ],    [ 2, 2, −0.5, 0, 0.5, 5.3 ] ];We are required to write a JavaScript function that takes in one such nested array of numbers. The function should combine all the numbers in the nested array to form a single string.In the resulting string, the adjacent numbers should be separated by a whitespaces and elements of two adjacent arrays should be separated by a comma.ExampleThe code for ...

Read More

Flat array of objects to tree in JavaScript

AmitDiwan
AmitDiwan
Updated on 21-Nov-2020 802 Views

Suppose, we have an array of objects like this −const arr = [    { id: '1', name: 'name 1', parentId: null },    { id: '2', name: 'name 2', parentId: null },    { id: '2_1', name: 'name 2_1', parentId: '2' },    { id: '2_2', name: 'name 2_2', parentId: '2' },    { id: '3', name: 'name 3', parentId: null },    { id: '4', name: 'name 4', parentId: null },    { id: '5', name: 'name 5', parentId: null },    { id: '6', name: 'name 6', parentId: null },    { id: '7', name: 'name 7', ...

Read More
Showing 3931–3940 of 6,517 articles
« Prev 1 392 393 394 395 396 652 Next »
Advertisements