JSON for dictionaries in JavaScript?

In my JS code, I need to convert the JSON response from the server to a dictionary so that I can access them by keywords. Here is the situation:

Let's say this is a JSON response from my server:

{ 'status': 'success', 'employees' : [ { 'id': 12, 'name': 'Tom', 'department': 'finance' }, { 'id': 34, 'name': 'Dick', 'department': 'admin' }, { 'id': 56, 'name': 'Harry', 'department': 'marketing' } ] } 

Now I need to create a dictionary variable so that the key is id and the value is the name (say), so that I can access them variable.id or variable [id_value] (from the loop).

How can this be achieved? Your help is much appreciated.

Thanks Vivek Ragunathan

+6
source share
4 answers

Note that this is not valid JSON: you need to use double quotes, not single quotes.

Assuming you fixed this, and that the JSON has already been extracted to the jsonResult string variable, you need to parse this to get the object using JSON.parse () :

 var result = JSON.parse(jsonResult); 

You can build a dictionary from there:

 var employees = {}; for (var i = 0, emp; i < result.employees.length; i++) { emp = result.employees[i]; employees[ emp.id ] = emp; } console.log(employees[56].name); // logs "Harry" console.log(employees[56].department); // logs "marketing" 

You said "and value is the name (let's say)" - if you don't need department values, then in the for loop above employees[ emp.id ] = emp.name; and then (obviously) employees[56] will give you "Harry."

Demo: http://jsfiddle.net/RnmFn/1/

+11
source

Well, if JSON is already parsed on Object :

 var data = JSON.parse('{"status":"success","employees":[{},{},{}]}'); // abridged 

You can run the data.employees to create a dictionary ( Object ):

 var employees = {}; for (var i = 0; i < data.employees.length; i++) { employees[data.employees[i].id] = data.employees[i]; } 

Then, given employeeId :

 var employee = employees[employeeId]; 
+2
source

You can write a javascript function to get an employee by id from the list of employees:

 function getEmployee(id, employees) { for (var i = 0; i < employees.length; i++) { var employee = employees[i]; if (employee.id === id) { return employee; } } return null; } 

and then use this function in the response received from the server after parsing the JSON string back to the javascript object:

 var json = 'the JSON string you got from the server'; var obj = JSON.parse(json); var employees = obj.employees; var employee = getEmployee(34, employees); if (employee != null) { alert(employee.name); } 
+1
source

Something like this allows you to create a dictionary on one line and specify the key and value to use, or at least the key in which the value becomes the entire object.

 function toDictionary(items, key, value) { var dictionary = {}; if (items) { for (var i = 0; i < items.length; i++) { var item = items[i]; dictionary[key(item)] = value ? value(item) : item; } } return dictionary; } 

Usage example:

 var dictionary = toDictionary(result.employees, function (record) { return record.id; }); var employee = dictionary[34]; if (employee) { // do something with employee object } 
0
source

Source: https://habr.com/ru/post/948816/


All Articles