4

Obtain the total number of specific keys from an associative array

 2 years ago
source link: https://www.codesd.com/item/obtain-the-total-number-of-specific-keys-from-an-associative-array.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

Obtain the total number of specific keys from an associative array

advertisements

Please how do i get the count of all "COLOR: RED" in this type of record? Actually the the data is a firebase json object

myarray =
    {
      "CAR": {
        "COLOR": "RED",
        "ID": "41.203.65.171",
        "rating": 5
      },
      "BIKE": {
        "COLOR": "BLUE",
        "ID": "41.203.65.171",
        "rating": 8
      },
      "PLANE": {
        "COLOR": "RED",
        "ID": "41.203.65.171",
        "rating": 3
      },

i tried this:

var count = 0;
jQuery.each(myarray, function (key, value) {
    if (key == "COLOR" && value == "RED")) {
        counts[value]++;
    } else {
        counts = 0;
    }
});

the above has been wrong and thats whhy i need help,

i expect is someting like red = 2;


If we start with your object like this:

var data = {
  "CAR": {
    "COLOR": "RED",
    "ID": "41.203.65.171",
    "rating": 5
  },
  "BIKE": {
    "COLOR": "BLUE",
    "ID": "41.203.65.171",
    "rating": 8
  },
  "PLANE": {
    "COLOR": "RED",
    "ID": "41.203.65.171",
    "rating": 3
  }
};

Then you can count the number of objects that have COLOR=RED with something like this:

// First determine all the vehicletypes in an array
var vehicleTypes = Object.keys(data); // [ "CAR", "BIKE", "PLANE" ]

// Next, filter that array to only contain the RED vehicle types: [ "CAR", "PLANE" ]
var redVehicleTypes = vehicleTypes.filter(function(vehicleType) {
  return data[vehicleType].COLOR == "RED"
});

// Finally, count the number of elements in the array
var redVehicleCount = redVehicleTypes.length;

Note that this solution doesn't use jQuery, Firebase or Angular.

Update

A solution that uses jQuery and is closer to your try:

var count = 0;
jQuery.each(data, function (key, value) {
    if (value["COLOR"] == "RED") {
        console.log("The "+key+" is red");
        count++;
    }
});
console.log(count);

The biggest change is in realizing the the each loops over vehicles, so you can simply check value["COLOR"] == "RED".

Note that picking good variable names is crucial to being able to understand the code that you write. So in the snippet above I already replaced your myArray with data, since (as some commenters pointed out) your data structure is not an Array. I would also recommend changing the generic key and value to vehicleType and vehicleData:

var count = 0;
jQuery.each(data, function (vehicleType, vehicleData) {
    if (vehicleData.COLOR == "RED") {
        console.log("The "+vehicleType+" is red");
        count++;
    }
});
console.log(count);


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK