Link to home
Start Free TrialLog in
Avatar of interclubs
interclubs

asked on

JavaScript function to check CSV against some JSON

So I am looking for a function that I can use like this:

checkPermission('email,status_update,publish_stream,offline_access');

function checkPermission(csvList) {
    jsonList = '...';
}

So inside the function it would loop through some JSON (below) and compare each item in csvList to make sure it appears in the JSON. If each item in the csvList appears in the JSON it returns true, if not false...

JSON:
{"extended":["status_update","photo_upload","video_upload","offline_access","email","create_note","share_item","publish_stream","contact_email","manage_pages"],"user":["read_stream","publish_stream","access_private_data"],"friends":["read_stream","access_private_data"]}
Avatar of Gurvinder Pal Singh
Gurvinder Pal Singh
Flag of India image

Your JSON can be read as an array of following elements
extended -- an array of different permissions
user -- an array of different permissions
friends -- an array of different permissions

Now what you want to do is,
1) take each elements from csv
var allCSVItems = csvList.split(",");
2) loop through them
for ( var counter = 0; counter < allCSVItems.length; counter ++)
{
}

3) inside the loop you will check for all the csv items, if they are present in any of the arrays in JSON object
see how to check for an element in array
http://stackoverflow.com/questions/1181575/javascript-determine-whether-an-array-contains-a-value
http://stackoverflow.com/questions/237104/javascript-array-containsobj

4) if any of the csv element is not found return false, else return true in the end

let me know if more help is required
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of interclubs
interclubs

ASKER

solved it
Thanks for the points!