Found 206 Articles for JSON

Group Similar Items in JSON in JavaScript

AmitDiwan
Updated on 21-Nov-2020 10:02:26

1K+ Views

Suppose, we have a JSON Array that contains data about some tickets like this −const arr = [    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "VIP Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to Event"    },    {       "quantity": "1",       "description": "Regular Ticket to ... Read More

Search by id and remove object from JSON array in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:55:56

2K+ Views

Suppose, we have an array of objects that contains data about some movies like this −const arr = [    {id: "1", name: "Snatch", type: "crime"},    {id: "2", name: "Witches of Eastwick", type: "comedy"},    {id: "3", name: "X-Men", type: "action"},    {id: "4", name: "Ordinary People", type: "drama"},    {id: "5", name: "Billy Elliot", type: "drama"},    {id: "6", name: "Toy Story", type: "children"} ];We are required to write a JavaScript function that takes in one such array as the first argument and an id string as the second argument. Then our function should search for the object ... Read More

Compare keys & values in a JSON object when one object has extra keys in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:41:17

2K+ Views

Suppose, we have two JSON objects like these −const obj1 = {a: "apple", b: "banana", c: "carrot"}; const obj2 = {a: "apple", e: "egg", b: "banana", c: "carrot", d: "dog"};We are required to write a JavaScript function that takes in two such objects. We want to be able to have a Boolean check comparing the two objects without having to remove data from either one.For example, if I were to use the data above, the Boolean check should return true because the values of the keys that are in both objects match.However, let’s say obj1 stays the same but obj2 ... Read More

Transform tree from DB format to JSON format in JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:47:22

347 Views

Suppose, we have an array of objects like this −const arr = [    {"id":7, "name":"Kuwait", "parentId":2},    {"id":4, "name":"Iraq", "parentId":2},     {"id":10, "name":"Qatar", "parentId":2},    {"id":2, "name":"Middle East", "parentId":1},    {"id":3, "name":"Bahrain", "parentId":2},    {"id":6, "name":"Jordan", "parentId":2},    {"id":8, "name":"Lebanon", "parentId":2},    {"id":1, "name":"Africa/Middle East", "parentId":null},       {"id":5, "name":"Israel", "parentId":2},     {"id":9, "name":"Oman", "parentId":2} ];We are required to write a JavaScript function that takes in one such array. The function should then prepare a new array that contains the objects grouped according to their parents.Therefore, the output should look like this −const output = [ ... Read More

Create array from JSON object JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:38:30

1K+ Views

Suppose, we have the following JSON object −const obj = {    "test1": [{       "1": {       "rssi": -25,    }    }, {       "2": {          "rssi": -25,       }    }],    "test2": [{       "15": {          "rssi": -10,       } }, {          "19": {          "rssi": -21,       }    }] };We are required to write a JavaScript function that takes in an object like this −The ... Read More

Merge JSON array date based JavaScript

AmitDiwan
Updated on 21-Nov-2020 06:16:25

591 Views

Suppose, we have the following array of objects −const arr = [    {       "date" : "2010-01-01",       "price" : 30    },    {       "date" : "2010-02-01",       "price" : 40    },    {       "date" : "2010-03-01",       "price" : 50    },    {       "date" : "2010-01-01",       "price2" : 45    },    {       "date" : "2010-05-01",       "price2" : 40    },    {       "date" : "2010-10-01", ... Read More

Convert JSON to another JSON format with recursion JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:42:20

1K+ Views

Suppose, we have the following JSON object −const obj = {    "context": {       "device": {          "localeCountryCode": "AX",          "datetime": "3047-09-29T07:09:52.498Z"       },       "currentLocation": {          "country": "KM",          "lon": -78789486,       }    } };We are required to write a JavaScript recursive function that initially takes in one such array.The function should split the above object into a "label" - "children" format.Therefore, the output for the above object should look like −const output = {   ... Read More

Deep Search JSON Object JavaScript

AmitDiwan
Updated on 21-Nov-2020 05:38:35

6K+ Views

Suppose we have the following nested JSON object −const obj = {    id: 1,    title: 'hello world',    child: {       id: null,       title: 'foobar',       child: {          id: null,          title: 'i should be in results array '       }    },    foo: {       id: null,       title: 'i should be in results array too!' },       deep: [       {          id: null,         ... Read More

How to turn a JSON object into a JavaScript array in JavaScript ?

AmitDiwan
Updated on 20-Nov-2020 09:59:28

459 Views

Suppose, we have this JSON object where index keys are mapped to some literals −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" };We are required to write a JavaScript function that takes in one such object and uses the object values to construct an array of literals.ExampleThe code for this will be −const obj = {    "0": "Rakesh",    "1": "Dinesh",    "2": "Mohit",    "3": "Rajan",    "4": "Ashish" }; const objectToArray = (obj) => {    const res = [];    const keys = Object.keys(obj);    keys.forEach(el => {       res[+el] = obj[el];    });    return res; }; console.log(objectToArray(obj));OutputAnd the output in the console will be −[ 'Rakesh', 'Dinesh', 'Mohit', 'Rajan', 'Ashish' ]

Finding the smallest value in a JSON object in JavaScript

AmitDiwan
Updated on 20-Nov-2020 09:57:09

640 Views

We are required to write a JavaScript function that takes in a JSON object as one and only argument.The JSON object has string keys mapped to some numbers. Our function should traverse through the object, find and return the smallest value from the object.ExampleThe code for this will be −const obj = {    "a": 4,    "b": 2,    "c": 5,    "d": 1,    "e": 3 }; const findSmallestValue = obj => {    const smallest = Object.keys(obj).reduce((acc, val) => {       return Math.min(acc, obj[val]);    }, Infinity);    return smallest; } console.log(findSmallestValue(obj));OutputAnd the output in the console will be −1

Advertisements