15

I have following JSON array I want to create object form status key count

[
  {
    "id": "65:0",    
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "RED"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
   {
    "id": "65:0",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  }
] 

Want to count status key value and create the following Object

{
 'ORANGE' : 3,
 'GREEN' : 4,
 'YELLOW' : 2,
 'RED' : 1,
}
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
Mehul Mali
  • 3,084
  • 4
  • 14
  • 28

3 Answers3

30

Use Array#reduce method

var res = data.reduce(function(obj, v) {
  // increment or set the property
  // `(obj[v.status] || 0)` returns the property value if defined
  // or 0 ( since `undefined` is a falsy value
  obj[v.status] = (obj[v.status] || 0) + 1;
  // return the updated object
  return obj;
  // set the initial value as an object
}, {})

var data = [{
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "YELLOW"
}, {
  "id": "65:0",
  "status": "RED"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "YELLOW"
}, {
  "id": "65:0",
  "status": "GREEN"
}];

var res = data.reduce(function(obj, v) {
  obj[v.status] = (obj[v.status] || 0) + 1;
  return obj;
}, {})

console.log(res);

Although you can use Array#forEach method with the same code.

var res = {};
data.forEach(function(v) {
  res[v.status] = (res[v.status] || 0) + 1;
})

var data = [{
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "YELLOW"
}, {
  "id": "65:0",
  "status": "RED"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "GREEN"
}, {
  "id": "65:0",
  "status": "ORANGE"
}, {
  "id": "65:0",
  "status": "YELLOW"
}, {
  "id": "65:0",
  "status": "GREEN"
}];

var res = {};
data.forEach(function(v) {
  res[v.status] = (res[v.status] || 0) + 1;
})

console.log(res);
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188
  • could you tell me how to push the output to object array so we can use it later (in your Array#forEach method )?Thanks – user1788736 Mar 23 '19 at 13:27
  • @user1788736 : what you mean by object array... something like `[{...},{...},....]` – Pranav C Balan Mar 23 '19 at 13:31
  • Thanks for your quick reply. Yes like this [{statusType:"orange",statusSize:3},{statusType:"green",statusSize:4},{..}..]. I tried this inside for each loop but it did not store those information MyObjectArray.push({ statusType: res[v.status], statusSize: (res[v.status] || 0) + 1 }); – user1788736 Mar 23 '19 at 13:41
  • @user1788736 : share your data – Pranav C Balan Mar 23 '19 at 13:43
  • var MyObjectArray = [ {userId:"10",userName:"david",displayUrl:"https://www.somesite.com/1david.jpg"}, {userId:"12",userName:"edward",displayUrl:"https://www.somesite.com/1edward.jpg"}, {userId:"10",userName:"david",displayUrl:"https://www.somesite.com/2david.jpg"}, {userId:"12",userName:"edward",displayUrl:"https://www.somesite.com/2edward.jpg"}, {userId:"10",userName:"david",displayUrl:"https://www.somesite.com/3david.jpg"} ]; and i want to push this info to another object array: var MyNewObjectArray =[ {userId:10,repeatedTimes:3},{userId:12,repeatedTimes:2}] – user1788736 Mar 23 '19 at 13:53
  • @user1788736 `let ref = {}; let res = []; data.forEach(function(o) { if (!(o.userId in ref)) res[ref[o.userId] = res.length] = { userId: o.userId, repeatedTimes: 0 } res[ref[o.userId]].repeatedTimes++; }) console.log(res);` – Pranav C Balan Mar 23 '19 at 14:07
  • can i use var instead of let for decaring the object arrays ? i want to be able to use them in other functions as well? – user1788736 Mar 23 '19 at 18:50
  • @user1788736 : yeah sure u can – Pranav C Balan Mar 23 '19 at 18:58
  • Please help me on this https://stackoverflow.com/questions/72084738/count-object-based-on-status-and-shop-using-javascript – its me May 02 '22 at 09:39
4

var obj = [
  {
    "id": "65:0",    
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "name": "BIU",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "name": "BIU",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "RED"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
   {
    "id": "65:0",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  }
] ;

var rez={};
obj.forEach(function(item){
  rez[item.status] ? rez[item.status]++ :  rez[item.status] = 1;
});
console.log(rez);
Vladu Ionut
  • 8,075
  • 1
  • 19
  • 30
1

Do you want this output format?

var json=[
  {
    "id": "65:0",    
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "RED"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  },
   {
    "id": "65:0",
    "status": "ORANGE"
  },
  {
    "id": "65:0",
    "status": "YELLOW"
  },
  {
    "id": "65:0",
    "status": "GREEN"
  }
] ;

var obj={};
json.forEach(function(item){
obj[item.status]? obj[item.status]++ : obj[item.status]=1;
});

console.log(obj)  
varsha ghodki
  • 201
  • 1
  • 7