0

my array looks like that:

var arr = [a,b,c,d,d,e,a,b,c,f,g,h,h,h,e,a];

How to create object from array? array value is become key of object and count duplicate value is become value of object

I want to get following object

{
  "a" : 3
  "b" : 1
  "c" : 2
  "d" : 2
}
1

2 Answers 2

3

Is this what you're trying to achieve? https://jsfiddle.net/yf184qob/

var arr = ['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];
var obj = {};
for(var a in arr){
    var temp = arr[a];
    if(typeof obj[temp] == "undefined"){
        obj[temp] = 0;
    }
    obj[temp]++;
}

console.log(obj);
Sign up to request clarification or add additional context in comments.

Comments

0
var arr =['a','b','c','d','d','e','a','b','c','f','g','h','h','h','e','a'];


    var cv ={};
    for(var i=0;i<arr.length;i++)
    {
        if (!(arr[i] in cv))
        {
            cv[arr[i]] =1

        }else
        {
            cv[arr[i]]=cv[arr[i]]+1;
        }
    }

Comments

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.