Moving var into external json file

Im getting stuck on the syntax and would appricate any help and a litle explanation on somthing that is so simple I’m sure.

I want to change this part of an inline script so that i can move the data into a json file

var data = [
{"id":"1","employee_name":"Tiger Nixon","employee_salary":"320800","employee_age":"61","profile_image":"default_profile.png"},{"id":"2","employee_name":"Garrett Winters","employee_salary":"434343","employee_age":"63","profile_image":"default_profile.png"},{"id":"3","employee_name":"Ashton Cox","employee_salary":"86000","employee_age":"66","profile_image":"default_profile.png"},{"id":"4","employee_name":"Cedric Kelly","employee_salary":"433060","employee_age":"22","profile_image":"default_profile.png"},{"id":"5","employee_name":"Airi Satou","employee_salary":"162700","employee_age":"33","profile_image":"default_profile.png"},{"id":"6","employee_name":"Brielle Williamson","employee_salary":"372000","employee_age":"61","profile_image":"default_profile.png"},{"id":"7","employee_name":"Herrod Chandler","employee_salary":"137500","employee_age":"59","profile_image":"default_profile.png"},{"id":"8","employee_name":"Rhona Davidson","employee_salary":"327900","employee_age":"55","profile_image":"default_profile.png"},{"id":"9","employee_name":"Colleen Hurst","employee_salary":"205500","employee_age":"39","profile_image":"default_profile.png"},{"id":"10","employee_name":"Sonya Frost","employee_salary":"103600","employee_age":"23","profile_image":"default_profile.png"}];

$('#txt-search').keyup(function(){
            var searchField = $(this).val();
			if(searchField === '')  {
				$('#filter-records').html('');
				return;
			}

Just store the data in a json file, then use getJSON.

var data = []

$.getJSON('datafile.json', function(jsonData) {
    data = data.concat(jsonData)
})

Remember that getJSON, or any AJAX call, is asynchronous so it’s not going to be available right away. I don’t see data being used in your snippet at all.

2 Likes

Thanks - also is there a way I could add a simple link to open a map based on the lat longs that I have stored in the json?

Yes, but that’s a totally different question. You should make a new thread.

1 Like

Fair point!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.