---
title: "How to collect values ​​in an array using Jquery?"  
description: "How to collect values ​​in an array using Jquery?"  
author: "Anurag Sharma"  
published: 2014-10-31  
updated: 2014-10-31  
canonical: https://www.mindstick.com/forum/2476/how-to-collect-values-in-an-array-using-jquery  
category: "jquery"  
tags: ["javascript"]  
reading_time: 2 minutes  

---

# How to collect values ​​in an array using Jquery?

Hi guys

I need to save all the color values of the [elements](https://www.mindstick.com/forum/1440/wpf-button-with-multiple-text-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](https://www.mindstick.com/forum/157765/how-to-remove-duplicate-rows-in-sql) 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](https://www.mindstick.com/blog/23175/how-to-solve-neet-question-paper-in-less-time) is, how recommended to create the array? directly inside the $ ("*") or in a [dedicated](https://www.mindstick.com/articles/13048/questions-that-render-a-helping-hand-for-hiring-dedicated-wordpress-developer) [function as](https://www.mindstick.com/forum/212/can-be-declare-a-static-function-as-virtual) I'm [thinking](https://www.mindstick.com/blog/11889/how-to-change-your-thinking-paradigm)? also, i need than once removed duplicates in the new array "clean" [as well as](https://www.mindstick.com/interview/2481/can-you-write-a-java-class-that-could-be-used-both-as-an-applet-as-well-as-an-application) the rgb value I would have also given the number of times that value was in the original. Some example code?

## Replies

### Reply by Norman Reedus

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);
```

\


---

Original Source: https://www.mindstick.com/forum/2476/how-to-collect-values-in-an-array-using-jquery

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
