-4

I have two arrays that I create dynamically. The number of cells in both is the same (array.length is the same, its like key and value association). Here they are:

barData.labels["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"]

barData.datasets[0].data["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"]

I need to check if there are duplicate values in barData.labels, if so I need to sum the values in the barData.datasets[0].data and delete them. For example if have 2 Food entries I need to sum the values and then delete one of them.

How can I achieve that?

jeerbl
  • 7,537
  • 5
  • 25
  • 39
Rafael
  • 157
  • 3
  • 13
  • 2
    Can you include some code you wrote? – stile17 Sep 25 '15 at 07:54
  • It would be much, much easier to do this at the point of creation. Can you please add the code where you create these arrays to the question. Also note, as one array is designed to hold the key and the other a value an object would be more suited to this task. – Rory McCrossan Sep 25 '15 at 07:55

6 Answers6

3

Maybe you could go with a Javascript object here, instead of using two arrays. Also, doing it at the time of creation could be more efficient.

You would have something like this:

barData = {
  "Food": ["2", "8", "20", "200", "1", ...]
}

Therefore, if you have to insert another Food key, you can check if Food is already defined in the object barData and, if not, you create it otherwise you just add an element in the array corresponding to the Food key.

jeerbl
  • 7,537
  • 5
  • 25
  • 39
0

you can use a array.filter function

have a look here

jQuery function to get all unique elements from an array?

Community
  • 1
  • 1
rTJ
  • 19
  • 6
0

Lets consider you have two arrays like below:

var labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone", "Phone", "Status", "Status"];
var barData = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99", "1", "333", '77'];

Now you want an output like below:

var result = {
  "Food": ["2","8","20","200","1","300","400","500","77","7"]
  "Phone": ["99","1"],
  "Status": ["333","77"]
}

Following code will do that:

var tempArr = []
var result  = {};
for(index in labels){
   var elm    = labels[index];
   if(tempArr.indexOf(elm) > -1){
      result[elm].push(barData[index])
   }
   else{
      tempArr.push(elm);
      result[elm] = [barData[index]];
   }
}
console.log(result);

Again if you want to sum them up then you can change a bit like below:

var tempArr = []
var result  = {};
for(index in labels){
   var elm    = labels[index];
   if(tempArr.indexOf(elm) > -1){
      result[elm]= parseInt(barData[index]) + parseInt(result[elm]);
   }
   else{
      tempArr.push(elm);
      result[elm] = barData[index];
   }
}
console.log(result);
Beroza Paul
  • 2,047
  • 1
  • 16
  • 16
0

This is a straightforward application of using a map to keep track of duplicates, so you know when to add and where to sum.

Something like this:

var ls = [ ... ];
var vs = [ ... ];
var dups = {};
var lls = [];
var vvs = [];
for(var i = 0, len = vs.length; i < len; i++) {
    var l = ls[i]; 
    var v = vs[i];
    if(dups.hasOwnProperty(l)) {
        var ii = dups[l];
        vvs += vs[ii];
    } else {
        var ii = vvs.length;
        dups[l] = ii;
        lls[ii] = l;
        vvs[ii] = v;
    }
}
sh0rug0ru
  • 1,596
  • 9
  • 9
0

What you could do is add your values to a temporary object, summing your values as you go, and then convert it back into the format of your original array.

// Creating your original array
var barData = {};
barData.labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"];
barData.datasets = [];
barData.datasets[0] = {};
barData.datasets[0].data = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"];
// Display original object
$("#original").text(JSON.stringify(barData));
var newBarData = {};
for (var i = 0; i < barData.labels.length; i++) {
  if (newBarData[barData.labels[i]] === undefined) {
    newBarData[barData.labels[i]] = {};
    newBarData[barData.labels[i]].labels = barData.labels[i];
    newBarData[barData.labels[i]].value = parseInt(barData.datasets[0].data[i]);
  } else
    newBarData[barData.labels[i]].value = newBarData[barData.labels[i]].value + parseInt(barData.datasets[0].data[i]);
}
//Convert back to original format
barData = {};
barData.labels = [];
barData.datasets = [];
barData.datasets[0] = {};
barData.datasets[0].data = [];
$.each(newBarData, function(index, me) {
  barData.labels.push(me.labels);
  barData.datasets[0].data.push(me.value);
});

$("#result").text(JSON.stringify(barData));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div id="original"></div>
<div id="result"></div>
John C
  • 3,052
  • 3
  • 34
  • 47
0

I suggest to build an object with the distribution of the items and then recreate the labels and count.

var labels = ["Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Food", "Phone"],
    data = ["2", "8", "20", "200", "1", "300", "400", "500", "77", "7", "99"],
    distribution = labels.reduce(function (r, a, i) {
        r[a] = (r[a] || 0) + +data[i];
        return r;
    }, {});

Object.keys(distribution).forEach(function (k, i) {
    if (!i) {
        labels = [];
        data = [];
    }
    labels.push(k);
    data.push(distribution[k]);
});

document.write('<pre>' + JSON.stringify(labels, 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(data, 0, 4) + '</pre>');
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392