...

View Full Version : Compare 2 arrays



hiyatran
12-16-2011, 01:52 AM
I have 2 arrays and I would like to compare the 2 arrays.
If an element in array 1 is NOT in array 2 then I would like to display that element.

In this case, I should only display the letter "c" but it doesn't work and I don't know why??
Here's my code:



<html><head>
<script type="text/javascript">
function getValue(id){
var x=new Array("a","b","c","d","e");
var y=new Array("a","b","3","d","e");
var str="";

for (var i=0; i<x.length; i++){
for (var j=0; j<y.length; j++){
if (x[i] == y[j]){
break;
}else{
//Check if reach the last element in the array 2
//If yes, then display that element in array 1 b/c not in array 2
if (y[j] == y.length-1){
str += x[i];
}
}
}
}
document.getElementById(id).innerHTML = str;

}


function init(){
getValue("info");
}
</script>
</head>

<body onload="init()">
<h2 id="info"></h2>
</body>
</html>

Dormilich
12-16-2011, 02:13 AM
I wouldn't use a nested loop ...

for (var l = x.length; l--;) {
if (-1 === y.indexOf(x[l])) {
str += x[l];
}
}

Old Pedant
12-16-2011, 02:29 AM
This makes no sense:

if (y[j] == y.length-1){

That would be doing, for example,

if (y[4] == 4){

And in any case it is unneeded.



function getValue(id)
{
var x=new Array("a","b","c","d","e");
var y=new Array("a","b","3","d","e");
var notFounds = [];

for (var xi=0; xi < x.length; xi++)
{
var xtest = x[xi];
for (var yi=0; yi<y.length; yi++)
{
if ( xtest == y[yi])
{
xtest = null;
break;
}
}
if ( xtest != null ) notFounds.push(xtest);
}
document.getElementById(id).innerHTML = notFounds.join(",");

}

Old Pedant
12-16-2011, 02:33 AM
Older MSIE doesn't have Array.indexOf, so if you need this to work universally, do use a nested loop.

Dormilich
12-16-2011, 11:56 AM
then I'd rather add an indexOf() method to the Array prototype than making it more complicated. older versions of IE are such a nuissance.

Old Pedant
12-16-2011, 11:36 PM
It depends on what this is for. If it's something that is done once per page, and for not many array elements, it's actually going to be more work to create the indexOf on the Array prototype than it's worth. If it's done on many pages and/or involves large arrays, then yeah, I'd add to the prototype. The performance on older IE will suck, but it would anyway.

cuzMazn
12-17-2011, 07:56 AM
I have 2 arrays and I would like to compare the 2 arrays.
If an element in array 1 is NOT in array 2 then I would like to display that element.

In this case, I should only display the letter "c" but it doesn't work and I don't know why??
Here's my code:



<html><head>
<script type="text/javascript">
function getValue(id){
var x=new Array("a","b","c","d","e");
var y=new Array("a","b","3","d","e");
var str="";

for (var i=0; i<x.length; i++){
for (var j=0; j<y.length; j++){
if (x[i] == y[j]){
break;
}else{
//Check if reach the last element in the array 2
//If yes, then display that element in array 1 b/c not in array 2
if (y[j] == y.length-1){
str += x[i];
}
}
}
}
document.getElementById(id).innerHTML = str;

}


function init(){
getValue("info");
}
</script>
</head>

<body onload="init()">
<h2 id="info"></h2>
</body>
</html>

Hi there,

Please take a look at this:



function isInArray($e, $a) {
for (var $j in $a) {
if ($a[$j] == $e) {
return true;
};
};
return false;
};

function dif($a, $b) {
var $aNotB = [];
var $bNotA = [];
for (var $k in $a) {
if (!isInArray($a[$k], $b)) {
$aNotB[$aNotB.length] = $a[$k];
};
};
for (var $k in $b) {
if (!isInArray($b[$k], $a)) {
$bNotA[$bNotA.length] = $b[$k];
};
};
return [$aNotB, $bNotA];
};


The function dif(Array1, Array2) will return an array. The first element of that array will be an array which contains values that appear in the Array1 but not in Array2. The second element of that array will be an array which contains values that appear in the Array2 but not Array1.

Here is the test:



var a = [1, 3, 4, 8];
var b = [2, 3, 8, 9];
var result = dif(a, b);
document.write("Element(s) that is in the first Array but not in the second Array: " + result[0]);
document.write("<br>Element(s) that is in the second Array but not in the first Array: " + result[1]);


Result:


Element(s) that is in the first Array but not in the second Array: 1,4
Element(s) that is in the second Array but not in the first Array: 2,9


I hope this help.

Tim_



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum