How to collect values ​​in an array using Jquery?

Total Post:40

Points:280
 1356  View(s)
Ratings:
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?

  1. Post:45

    Points:315
    Re: How to collect values ​​in an array using Jquery?

    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);

     


      Modified On Apr-06-2018 02:04:56 AM

Answer