Tuesday, 15 September 2015

javascript - Map the Json Array values -



javascript - Map the Json Array values -

trying map 2 array values. 1 array values coming local json file , other array values coming service or backend.

local json:

var localjsonarray = { "records": { "cat1": [{ "id": 1234, "label": "a" }, { "id": 2345, "label": "b" }], "cat2": { "id": 12345, "label": "c" } } }

backend array values:

i storing value coming backend as:

var backendarray =[0: "1234", 1: "3456", 2:"4567"];

js:

$.each( localjsonarray, function( key, value ) { var index = $.inarray( value, backendarray ); if( index != -1 ) { console.log( index ); } });

now how map id of local json id of backend json. if value matches loop should break else should value.

you can't create array var backendarray =[0: "1234", 1: "3456", 2:"4567"];

so way ... follow code above:

var localjsonarray = { "records": { "cat1": [{ "id": 1234, "label": "a" }, { "id": 2345, "label": "b" }], "cat2": { "id": 12345, "label": "c" } } }; var backendarray =["1234", "3456", "4567"]; $.each( localjsonarray['records'], function( a,b,c ) { if(!(b.length == undefined)){ for(var = 0;i < b.length; i++) { var index = $.inarray( b[i].id.tostring(), backendarray ); if( index != -1 ) { console.log( index ); } } } else{ var index = $.inarray( b.id.tostring(), backendarray ); if( index != -1 ) { console.log( index ); } } });

hope help!!! :)

javascript arrays json loops

No comments:

Post a Comment