Count number of items in an array of objects with a specific property value (JavaScript)

I have an array of objects. Note that "created" sometimes has the same value more than once. Also note that "status" is sometimes "0":

var array = [ {"created":"Jan 1","status":1}, {"created":"Jan 1","status":1}, {"created":"Jan 2","status":1}, {"created":"Jan 3","status":0}, {"created":"Jan 4","status":1}, {"created":"Jan 4","status":1} ];

I want a new a new array of objects like this:

var newArrayA = [ {"Jan 1":2}, {"Jan 2":1}, {"Jan 3":0}, {"Jan 4":2} ];

OR a multidimensional array like this (I'm not sure which is better for D3.js):

var newArrayB = [ ["Jan 1",2], ["Jan 2",1], ["Jan 3",0], ["Jan 4",2] ];

So, when "status" is "1", it counts the number of "created" that are the same. And when status is "0", that is also included.

How do I do this? Here's what I have so far JSFIDDLE. Is this how to approach it? Or am I missing some simple solution? Thanks!

--------------Solutions-------------

D3 would cope with either result format - it does slightly depend on what you want to do with it. Regardless, here is some example code for generating either format:

var result = {};
array.forEach(function(i) {
if (!result.hasOwnProperty(i.created)) {
result[i.created] = 0;
}
if (i.status === 1) {
result[i.created] += 1;
}
});

var newArrayA = Object.keys(result)
.map(function(k) {
var o={};
o[k] = result[k];
return o;
})

var newArrayB = Object.keys(result)
.map(function(k) { return [k,result[k]] })

Category:javascript Time:2018-12-01 Views:1

Related post

  • how to count number of items in an array android 2011-08-19

    How can I count the number of items in an array in android? here is the code that dose not work if (vehicleList.length = 1){ //do something } can anyone help? --------------Solutions------------- Use == for comparison.. if (vehicleList.length == 1){

  • jQuery create an array of objects with a specific class 2010-07-01

    Is there an array created somewhere of all the objects with a specific class? I want to find the sixth div with class="clCategory". Is there something like: $('.clCategory:sixth').css(...); or $('.clCategory')[6].css(...); Or do I have to create the

  • Finding parents where child collection does not contain an item with a specific property value 2010-04-28

    I am trying to get a list of parents where the child collection does not contain an item of a specific type. The LINQ equivalent would be something like: dataset.Where(x => x.Items.FirstOrDefault(y => y.Type.Code == "ABC") == null) The object m

  • How do I echo the number of items in an array? 2010-06-06

    A total newbie question I know. I'm sing Zend framework, and sending an array from the controller: $this->view->googleArray = $viewFeedArray; In the view, I have a foreach loop that is populating a table with the array contents. <?php foreac

  • Count number of items in NSArray with certain property (iPhone Programming) 2011-03-06

    I have an NSArray that looks like this: ArrayItem *thisone = [[ArrayItem alloc] initWithName:@"The Name" data1:@"Some data" data2:@"Some more data" image:@"theimage.jpg" range:0]; ArrayItem *thisone1 = [[ArrayItem alloc] initWithName:@"The Name" data

  • How do I see common Items between 2 array of Objects 2009-05-14

    How do I see common Items between 2 array of Objects. My intersect is not returning anything. The object is created from a Linq to SQL class. --------------Solutions------------- You will need to override the object's Equals method. You can find some

  • Count number of grid boxes which cover an object in an image 2013-05-15

    I have displayed grid boxes over an image. I want to count number of grid boxes which cover an object in that image for example . In this image I have displayed grid on image but i need to count how many girds have been taken to cover that object com

  • Gson and deserializing an array of objects with arrays in it 2010-09-21

    I am trying to use Gson to deserialize a json string returned from my webservice The structure would be returned as TypeDTO[]. where TypeDTO is like int id; String name; ArrayList<ItemDTO> items[] and ItemDTO is like int id; String name; Boolea

  • Issue Reversing Array of Objects with JS 2012-03-14

    I'm parsing JSON and getting an array of objects with javascript. I've been doing this to then append an element for each object: for(o in obj){ ... } But I realized that for a certain situation I want to go backwards through the array. So I tried th

  • Initializing array of objects with data from text file 2009-11-09

    I’m getting system error when I try to compile the code below on Visual C++ 2008 Express. What I’m trying to do is to initialize array of objects with data read from file. I think there is something wrong inside the while loop, because when I initial

  • JSON handling Array and object with same Java code 2011-09-02

    I have a simple question for android/java implementing JSON. Is there any method possible to handle an array or object with the same code. In my Json response there are many placed where JSON returns a single argument and some times many arguments in

  • PHP - count my quantity item in session array 2009-12-16

    How to count the item-qty and current code :- $q = $_POST['item-qty']; $i = count($q); $k = 0; while ($k < $i) { $select = 'SELECT * FROM location'; $query = $db->rq($select); $price = $db->fetch($query); if ($_POST['item-qty'][$k] < 3) {

  • A good way to look back arbitrary number of items in the Array.Fold in F# 2010-05-30

    In the folder function of the Array.Fold operation, i want to look back arbitrary number of items. The only way i can figure out is like this. let aFolder (dataArray, curIdx, result) (dataItem) = let N = numItemsToLookBack(result, dataItem) let recen

  • Count number of chars in char array including spaces until null char 2010-09-23

    I'm trying to count the number of chars in a char array including the space until the end of the string. The following compiles but doesn't return the correct value, I'm trying to use pointer arithmetic to interate through my array. int numberOfChars

  • Watir Webdriver counting number of items in a UL list 2011-08-30

    I've done a few searches and I'm unable to find a suitable answer. Basically I have an unordered list which can be of a varying length. I want to iterate through the list, do some other things and then come back and select the next item on the list.

  • Limit the number of items in an array 2012-01-18

    I have an array that gets processed through a foreach loop. foreach($images as $imageChoices) { Do stuff here } How do I limit the loop to only process the first 4 items in the array? --------------Solutions------------- The array_slice() function ca

  • Count number of items with property 2012-02-06

    I have list List<Custom> where Custom is like class Custom{ public int id; public String name; } How to get number of items which have name "Tom" ? Is there easier way than a for loop ? --------------Solutions------------- If you are going to f

  • Mergesort Implementation.. Counting number of inversions in an array 2012-03-19

    I'm taking an online class on Algorithms and trying to implement a mergesort implementation of finding the number of inversions in a list of numbers. But, I cant figure what Im doing wrong with my implementation as the number of inversions returned i

  • Dynamically printing out XML according to the number of items in an array 2011-12-13

    I have a function that is generating XML for a contract last web service(not my choice). public function generateXml($endorsements) { $xmlstr = ' <?xml version="1.0" encoding="utf-8" ?> <is:CaptureRQ xmlns:dg="urn:digimarc.com:SchemaTypes" D

Copyright (C) pcaskme.com, All Rights Reserved.

processed in 4.114 (s). 15 q(s)