한 배열의 각 요소를 다른 배열로 어떻게 확인합니까? 여기서 array2는 array1의 각 요소를 포함합니다.
암호:
function find(a, b) {
var ai=0, bi=0;
var result = [];
while( ai < a.length && bi < b.length )
{
if (a[ai] < b[bi] ){ ai++; }
else if (a[ai] > b[bi] ){ bi++; }
else /* they're equal */
{
result.push(a[ai]);
ai++;
bi++;
}
}
if(JSON.stringify(a)==JSON.stringify(result)){
return true;
}else if(JSON.stringify(b)==JSON.stringify(result)){
return true;
}else{
return false;
}
// return result;
}
var array1 = ["Area", "Code", "Date", "Invoice Amt", "Invoice No", "Party Address", "Party Name", "Pincode"];
var array2 = ["Area", "Code", "Date", "Invoice Amt", "Invoice No", "Name", "Party Address ", "Party Name", "Pincode"];
console.log(find(array1, array2)); //returns false
console.log(find(array2, array1)); // return false
모든 값을 하나씩 확인하려면 사용 map
하고 includes
배열에 array1의 값이 포함되어 있는지 확인할 수 있습니다.
var array1 = ["Area", "Code", "Date", "Invoice Amt", "Invoice No", "Party Address", "Party Name", "Pincode"];
var array2 = ["Area", "Code", "Date", "Invoice Amt", "Invoice No", "Name", "Party Address ", "Party Name", "Pincode"];
array1.map(x => console.log(array2.includes(x)))
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다