Hi all. I have script that use two arrays. It pushes original array values to the newly created array. it dont pushes into new array duplicate values.
for Ex; the orginal array is var arr=new array will be var newArr=
the second three will not copied. but one disadvantage is that when it first meet the duplicated value after it it will not push another values into new array
i would like let it does like that
for ex . var arr=
var new arr=;
below is script

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
    "http://www.w3.org/TR/html4/strict.dtd"
    >
<html>
<head>
    <title><!-- Insert your title here --></title>
    <script>
    var unique = function(origArr) {
    var newArr = [],

        origLen = origArr.length,
        found,
        x, y;

    for ( x = 0; x < origLen; x++ ) {
    //    found = undefined;
        for ( y = 0; y < newArr.length; y++ ) {
            if ( origArr[x] === newArr[y] ) {
              found = true;
              break;
            }
        }
        if ( !found) newArr.push( origArr[x] );
      
    }
        var az=[newArr,origArr];
  return newArr;
};

// Test it out
var myarray = ['jeffrey', 'allie', 'patty', 'damon', 'zach', 'jeffrey', 'allie','samir'];
var az = unique(myarray);
//document.write(az); // jeffrey, allie, patty, damon, zach
  
    
    
    
    </script>
</head>
<body>
    
    <input type="button" value="goster" onclick="document.write(az)">
</body>
</html>

thanks for attention

Recommended Answers

All 2 Replies

The problem you encounter right now is that you declare 'found' outside the for-loop scope. Once it is changed to 'true', it will always be 'true'. You need to initialize the value of 'found' before the checking loop. You did it, but it is incorrect.

Only modify your line 16 to 'found = false;' should do it.

Thank you very much. you solved problem

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.