admin管理员组

文章数量:1417334

I have a JSON database with objects. Each one has properties with a specific assigned value: a, b or c.

[
  {
    "id": 1,
    "category": "a"
  },
  {
    "id": 2,
    "category": "b"
  },
  {
    "id": 3,
    "category": "c"
  },
  {
    "id": 4,
    "category": "a"
  },
  {
    "id": 5,
    "category": "a"
  },
  {
    "id": 5,
    "category": "b"
  }
]

I want to display something like:

There is a total of 6 items: a x 3, b x 2 and c x 1.

I know I have to use objectsinmyjsondatabase.length to get the total.

I'm wondering how is it possible to get the length (number) of objects that have a specific value?

I have a JSON database with objects. Each one has properties with a specific assigned value: a, b or c.

[
  {
    "id": 1,
    "category": "a"
  },
  {
    "id": 2,
    "category": "b"
  },
  {
    "id": 3,
    "category": "c"
  },
  {
    "id": 4,
    "category": "a"
  },
  {
    "id": 5,
    "category": "a"
  },
  {
    "id": 5,
    "category": "b"
  }
]

I want to display something like:

There is a total of 6 items: a x 3, b x 2 and c x 1.

I know I have to use objectsinmyjsondatabase.length to get the total.

I'm wondering how is it possible to get the length (number) of objects that have a specific value?

Share Improve this question edited Jul 29, 2017 at 11:37 user663031 asked Jul 27, 2017 at 21:06 JonathanJonathan 3751 gold badge7 silver badges18 bronze badges 1
  • If you are looking to do the count in the view, this answer will help you. – Nehal Commented Jul 27, 2017 at 21:14
Add a ment  | 

3 Answers 3

Reset to default 5

Define a function:

getCount(character) {
  return this.objects.filter(obj => obj.category === character).length;
}

and the call it as: this.getCount('a');

One way to solve your problem is to use map-reduce. Here is a quick solution. Hope that will solve your problem.

var data = [
  {
    "id": 1,
    "category": "a"
  },
  {
    "id": 2,
    "category": "b"
  },
  {
    "id": 3,
    "category": "c"
  },
  {
    "id": 4,
    "category": "a"
  },
  {
    "id": 5,
    "category": "a"
  },
  {
    "id": 5,
    "category": "b"
  }
];

// First get list of all categories
var categories = data.map(function(x) { return x["category"]; });

// Count no of items in each category
var countByCategories = categories.reduce(function(x, y) {
    if (typeof x !== "object") {
       var reduced = {};
       reduced[x] = 1;
       reduced[y] = 1;
       return reduced;
    }
    
    x[y] = (x[y] || 0) + 1;
    return x;
});

// Final build string that you want
var categoryStrings = [];
for (var category in countByCategories) {
   categoryStrings.push(category + ' x ' + countByCategories[category]); 
}

var msg = 'There is a total of ' + categories.length + ' items: ';
if (categoryStrings.length > 2) {
   msg = categoryStrings.slice(0, -1).join(', '); 
   msg += ' and ' + categoryStrings.slice(-1);
} else {
   msg = categoryStrings.join(', '); 
}

// print results
console.log(msg);

You easily can count objects which have specific value like this

let objects = [
    {
        "id": 1,
        "category": "a"
    },
    {
        "id": 2,
        "category": "b"
    },
    {
        "id": 3,
        "category": "c"
    },
    {
        "id": 4,
        "category": "a"
    },
    {
        "id": 5,
        "category": "a"
    },
    {
        "id": 5,
        "category": "b"
    }
];
var filtered = new Array();
objects.filter(function (t) {
    var found = filtered.some(function (el, index) {
        if (false == (t.category === el.category)) {
            return false;
        }
        filtered[index].count = filtered[index].count + 1;
        return true;

    }, filtered);
    if (false === found) {
        filtered.push({category: t.category, count: 1});
    }
}, filtered);
console.log('There is a total of ' + objects.length + ' items: '
    + filtered.map(function (t) {
        return t.category + ' x ' + t.count;
    })
);

本文标签: javascriptAngular How to get the count of the Object with specific valueStack Overflow