RSKMR
RSKMR

Reputation: 1892

Array Compare and get count

I am new in this. I have Multiple array list:

    var fruits1 = ["A", "B", "C", "D"];
    var fruits2 = ["O", "E", "F", "G"];
    var fruits3 = ["S", "D", "E", "S"];
    var fruits4 = ["A", "B", "Z", "A"];
    var fruits5 = ["A", "R", "U", "V"];
    var fruits6 = ["N", "B", "O", "M"];
    var fruits7 = ["A", "B", "N", "P"];
    var fruits8 = ["X", "C", "Z", "Z"];
    var fruits9 = ["X", "B", "X", "B"];

The array having number of times like following things.

    A - 5
    B - 6
    C - 2
    D - 2
    E - 2
    F - 1
    G - 1
    M - 1
    N - 2
    O - 2
    P - 1
    R - 1
    S - 2
    U - 1
    V - 1
    X - 3
    Z - 3

Need output like this using javascript/jquery

[{"B" : 6}, { "A" : 5}, {"X":3}, {"Z" : 3}, {"C" : 2}, { "D" : 2}, { "E" : 2}, { "N" : 2}, { "O" :2}, { "S" : 2}, { "F": 1 }, { "G" :1}, { "M" :1}, { "P" :1}, { "R" :1}, { "U" :1}, { "V" :1 }]

Can anyone have idea to do this?

Upvotes: 4

Views: 138

Answers (2)

Rick Hitchcock
Rick Hitchcock

Reputation: 35670

I may be overthinking this, but here's what I've come up with:

var fruits1 = ["A", "B", "C", "D"],
    fruits2 = ["O", "E", "F", "G"],
    fruits3 = ["S", "D", "E", "S"],
    fruits4 = ["A", "B", "Z", "A"],
    fruits5 = ["A", "R", "U", "V"],
    fruits6 = ["N", "B", "O", "M"],
    fruits7 = ["A", "B", "N", "P"],
    fruits8 = ["X", "C", "Z", "Z"],
    fruits9 = ["X", "B", "X", "B"],
    fruits= [].concat(fruits1, fruits2, fruits3, fruits4, fruits5, fruits6, fruits7, fruits8, fruits9),
    cnt= {},
    output= [];

fruits.forEach(function(val) {
  cnt[val]= (cnt[val] || 0)+1
});

Object.keys(cnt).forEach(function(val, idx) {
  var obj= {};
  obj[val]= cnt[val];
  output.push(obj);
});

output= output.sort(function(a, b) {
  var ak= Object.keys(a)[0],
      bk= Object.keys(b)[0],
      a1= a[ak],
      b1= b[bk];
  return a1 < b1 ? 1 : a1 > b1 ? -1 : 
         ak < bk ? -1 : ak > bk ? 1 : 0;
});

document.querySelector('#output').innerHTML= JSON.stringify(output);
<div id="output"></div>

The concat function joins all the arrays into a single array named fruit.

The fruits.forEach loop stores the total fruits in a cnt object, which looks like this: {"A": 5, "B": 6, "C": 2, ...}.

The Object.keys(cnt).forEach loop pushes the cnt keys into an output array.

The output.sort function then sorts the values, first by descending number of items, then by ascending key.

Upvotes: 3

Manoz
Manoz

Reputation: 6587

I suggest you to have only array of all the fruits, Then use the following mapping-

i.e. in your case I merged the array into single one-

var fruits = ["A", "B", "C", "D","O", "E", "F", "G","S", "D", "E", "S","A", "B", "Z", "A","A", "R", "U", "V","N", "B", "O", "M","A", "B", "N", "P","X", "C", "Z", "Z","X", "B", "X", "B"];

and then-

var counts = {};

for(var i = 0; i< fruits.length; i++) {
    var fruit = fruits[i];
    counts[fruit] = counts[fruit] ? counts[fruit]+1 : 1;

}

Demo

Upvotes: 1

Related Questions