javascript - Compare integer in the list of integer array -
in javascript, have list of integer arrays in single list. how can compare integer in list?
var childone = []; childone.push(3); childone.push(1); var childtwo = []; childtwo.push(4); childtwo.push(2); var main = []; main.push(childone); main.push(childtwo); how compare integers in kid arrays?
note: have json object above mentioned "main" object. task is, how find integers in kid array same or not?
you need iterate on main array , compare 2 items. used array.indexof. code returns true if arrays in main equal:
function match(arr) { var i, m = false; // checks if 2 arrays contain same items function intmatch(arrleft, arrright) { var l, m = arrleft.length === arrright.length; // if lenghts different no need check if (m) { // loop on left array for(l = 0; l < arrleft.length; l = l + 1) { // in right array if (arrright.indexof(arrleft[l]) === -1) { m = false; break; // exit loop } } } homecoming m; } // length-1 because compare 2 items for(i = 0; < arr.length - 1; = + 1) { // take item , next 1 m = intmatch(arr[i], arr[i + 1]); if (m === false) { break; // exit loop } } homecoming m; } here the jsfiddle in case need alter or test stuff.
javascript arrays
No comments:
Post a Comment