Home > DeveloperSection > Forums > How to collect values ​​in an array using Jquery?
Anurag Sharma
Anurag Sharma

Total Post:40

Points:280
Posted on    October-31-2014 12:44 AM

 JQuery JavaScript 
Ratings:


 1 Reply(s)
 689  View(s)
Rate this:

Hi guys

I need to save all the color values ​​of the elements in the pages of my site and put them in a database. I thought I'd do it this way:

First thing I'm going to pick up the rgb values ​​of each element so

$("*").each(function (e) {

     createColorArray($(this).css('backgroundColor'));

});

then in the function createColorArray store into an array all the values ​​that are passed

function createColorArray(rgbColor) {

     //Create rgb array

}

and finally remove duplicate items from my array

function removeDoupe(ar) {

        var temp = {};

        for (var i = 0; i < ar.length; i++)

            temp[ar[i]] = true;

        var r = [];

        for (var k in temp)

            r.push(k);

        return r;

    }

now my question is, how recommended to create the array? directly inside the $ ("*") or in a dedicated function as I'm thinking? also i need than once removed duplicates in the new array "clean" as well as the rgb value I would have also given the number of times that value was in the original. Some example code?



Norman Reedus
Norman Reedus

Total Post:45

Points:315
Posted on    October-31-2014 12:45 AM

As I mentioned in the comments, why not check for duplicates earlier? A simple example:

var colors = [];

 

    $('*').each(function (i, el) {

 

        var $element = $(el),

            color = $element.css('background-color');

 

        if (!~$.inArray(color, colors))

            colors.push(color);

    });

 

    console.log(colors);


Don't want to miss updates? Please click the below button!

Follow MindStick