Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts

    Pushing array into an Object

    Hello All,

    I'm trying to do the following:
    - Inside my array I want to keep track of what is a dupe but I don't know how to go about it.
    - Since I have to 2 letter a I want it to say something like { 'a', count=2 }

    This is what I have so far:
    Code:
    array = ['a','b','c','d','c']
    var alphabet = {};
    var count = [];
    
    for (a=0; a<array.length; a++){
    	myCount = 0;
    
    	alphabet.value = array[a];
    	alphabet.count = myCount;
    	count.push(alphabet);	
    }
    console.log(alphabet); // returns Object { value="c", count=0 }

  • #2
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,146
    Thanks
    39
    Thanked 505 Times in 499 Posts

    Lightbulb

    Code:
    <html>
    <head>
    <title> Untitled </title>
    <script type="text/javascript">
    var alphaArray = ['a','b','c','d','c'];
    var counts = [];
    for (var a=0; a<alphaArray.length; a++){
      if (counts[alphaArray[a]] == undefined) { counts[alphaArray[a]] = 1; }
                                         else { counts[alphaArray[a]]++; }
    }
    var str = 'Occurances in array\n\n';
    for (var indx in counts) { str += indx+' : '+counts[indx]+'\n'; }
    alert(str);
    </script>
    </head>
    <body>
    
    </body>
    </html>

  • Users who have thanked jmrker for this post:

    bunzu (07-19-2012)

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    3
    Thanked 0 Times in 0 Posts
    Thanks jmrker.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,310
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    The following will remove duplicates from an aray:-

    Code:
    <script type = "text/javascript">
    
    removeDuplicated = function(a, s){
        var p, i, j;
        if(s) for(i = a.length; i > 1;){
            if(a[--i] === a[i - 1]){
                for(p = i - 1; p-- && a[i] === a[p];);
                i -= a.splice(p + 1, i - p - 1).length;
            }
        }
        else for(i = a.length; i;){
            for(p = --i; p > 0;)
                if(a[i] === a[--p]){
                    for(j = p; --p && a[i] === a[p];);
                    i -= a.splice(p + 1, j - p).length;
                }
        }
        return a;
    };
    </script>
    
    
    
    <script type = "text/javascript">
    
    var arr = ["Philip", "Tom","Peter", "Tom", "Tony", "Philip", "Mary"];
    b = arr.slice().sort();
    
    document.write("<h2>Original Array</h2>A = ", arr.join(","),
        "<br />removeDuplicated(A) = ", removeDuplicated(arr),
        "<br /><h2>Original Sorted Array</h2>B = ", b.join(","),
        "<br />removeDuplicated(B, true) = ", removeDuplicated(b, true)
    );
    
    </script>

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •