0

I have some array of objects and in that few data. My input is like :

My actual code is :

buildcount: function(store2){
   var datacount = store2.data.items;
    for(var i=0; i<datacount.length; i++){
        var x = datacount[i].data
},

Here from store I am hgetting x which is array of object and each object have

obj{
    STU: "Study1", SUB: "Subject1", EXL: "Yes"}
}

Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study2", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study1", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study3", SUB: "Subject1", EXL: "Yes"}
Object {STU: "Study2", SUB: "Subject1", EXL: "Yes"}

I want to count all the unique element and store in array with count. In javascript.

Output : object {"Study1 : 3", "Study2" : 2, "Study3":4}

2
  • 1
    What have you tried so far? Please share it. I can't see how you retrieve these objects as input. Commented Jul 3, 2016 at 12:16
  • Given your input dataset, the correct output would be object {"Study1 : 3", "Study2" : 2, "Study3":3} Commented Jul 3, 2016 at 12:21

6 Answers 6

2

You can do this with reduce

var ar =  [{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study2","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study1","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study3","SUB":"Subject1","EXL":"Yes"},{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}]

var result = ar.reduce(function(o, e) {
  return o[e.STU] = (o[e.STU] || 0) + 1, o
}, {});

console.log(result)

Sign up to request clarification or add additional context in comments.

4 Comments

Do I need to write reduce function anywhere.
I am not sure what do you mean by that?
I am using EXTjs framework in that reduce() is not working.
Reduce is pure javascript function.
2

You can use a combo of filter and indexOf:

function arrayUniq(arr) {
    return arr.filter(function(ele, pos) {
        return arr.indexOf(ele) == pos;
    });
}
var array1 =  
['{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study1","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study3","SUB":"Subject1","EXL":"Yes"}','{"STU":"Study2","SUB":"Subject1","EXL":"Yes"}'];

var unique = arrayUniq(array1);

console.log(unique);

Comments

2

Best to do it without side-effects and just using Reduce. Also keep it readable for your colleagues as well. I find I use this code snippet as a pure function a lot recently. I hope it helps you as well. Bin: https://jsbin.com/bajavucowa/edit?js,console

    var arr = [{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study2", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study1", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study3", SUB: "Subject1", EXL: "Yes"},
    {STU: "Study2", SUB: "Subject1", EXL: "Yes"}]
    
    
    var reducer = function(acc, next) {
      if (!acc[next.STU]) {
        acc[next.STU] = 1;
      } else {
        acc[next.STU] = acc[next.STU] + 1;
      }
      return acc;
    };
    
    var result = arr.reduce(reducer, {});

    console.log(result)

Comments

2

You can do it like this :

var array = [{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study2", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study1", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study3", SUB: "Subject1", EXL: "Yes"},
{STU: "Study2", SUB: "Subject1", EXL: "Yes"}];


function count() {
    var result = {};

    for(var i = 0; i < array.length; ++i){
        for(var el in array[i]){
          if(result[array[i][el]] !== undefined)
            result[array[i][el]] += 1;
         else
            result[array[i][el]] = 1;
      }
    }

    return result;
}

The jsfiddle is here : https://jsfiddle.net/1pfgjaf5/3/

Comments

1

Not the most efficient way, buy easy to understand: You store a "hash" of your values in an object every time you found a new value.

var tab = [ {STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study2", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study1", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study3", SUB: "Subject1", EXL: "Yes"},{STU: "Study2", SUB: "Subject1", EXL: "Yes"}];


var result = {};
var kys;

for(var i=0; i < tab.length ; i++){
var concat = tab[i].STU + tab[i].SUB + tab[i].EXL;



kys = Object.keys(result);

if(kys.indexOf(concat) == -1){
window.console.log("element not found");
result[concat]=concat;

}else{
window.console.log("element found");
}
}
window.console.log(result);

https://jsfiddle.net/6q0w6fsc/

Comments

1
buildcount: function(store2){
   var datacount = store2.data.items;
var uniq = {}
    for(var i=0; i<datacount.length; i++){
        var x = datacount[i].data;
          x.forEach(function(obj) {
       uniq[obj.STU] = uniq[obj.STU] || 0;
    uniq[obj.STU]+=1;
    });
}
},    

3 Comments

sorry I am new in programming. In my code it is coming arr is undefined. may I know what is arr here.
My input is object and in object I have arrays, which I mention in code
From the question it looks like array of objects only, can you update the post with exact data

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.