Home > OS >  How to count number of lines containing a string in an API?
How to count number of lines containing a string in an API?

Time:09-11

I'm looking to count the number of times a crime has occurred on a street by counting the times the street is mentioned in a police statistics site then display it.

The display bit I can do, rather it's pulling the count into a variable that I'm struggling to understand where to start.

The data is: https://data.police.uk/api/crimes-street/all-crime?lat=51.728395&lng=-1.2314066

CodePudding user response:

that's it, folks

const allCrimes = [{
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.723879", "street": { "id": 1203542, "name": "On or near Elm Tree Close" }, "longitude": "-1.221349" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646934, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.725988", "street": { "id": 1203518, "name": "On or near Sports\/recreation Area" }, "longitude": "-1.211410" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647063, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.723362", "street": { "id": 1203546, "name": "On or near Champion Way" }, "longitude": "-1.219389" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646887, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.725683", "street": { "id": 1203528, "name": "On or near Oxford Road" }, "longitude": "-1.223634" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647032, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.733223", "street": { "id": 1204183, "name": "On or near Parking Area" }, "longitude": "-1.217713" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647491, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.724270", "street": { "id": 1203378, "name": "On or near Pedestrian Subway" }, "longitude": "-1.224730" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647022, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.725454", "street": { "id": 1203529, "name": "On or near Newman Road" }, "longitude": "-1.221684" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647008, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.724505", "street": { "id": 1203430, "name": "On or near Constance Norman Way" }, "longitude": "-1.231762" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647006, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.741460", "street": { "id": 1204314, "name": "On or near Catherine Street" }, "longitude": "-1.232879" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647957, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.736008", "street": { "id": 1204151, "name": "On or near Littlehay Road" }, "longitude": "-1.220069" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647691, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.735840", "street": { "id": 1204256, "name": "On or near Weirs Lane" }, "longitude": "-1.241070" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647682, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.730241", "street": { "id": 1204163, "name": "On or near The Grates" }, "longitude": "-1.216823" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647298, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.733223", "street": { "id": 1204183, "name": "On or near Parking Area" }, "longitude": "-1.217713" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647489, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.733223", "street": { "id": 1204183, "name": "On or near Parking Area" }, "longitude": "-1.217713" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647488, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.728327", "street": { "id": 1203508, "name": "On or near Bartholomew Road" }, "longitude": "-1.214294" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647262, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.734466", "street": { "id": 1204251, "name": "On or near Police Station" }, "longitude": "-1.213898" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647566, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.723879", "street": { "id": 1203542, "name": "On or near Elm Tree Close" }, "longitude": "-1.221349" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646925, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.737846", "street": { "id": 1204282, "name": "On or near Swinburne Road" }, "longitude": "-1.238342" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647752, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.737395", "street": { "id": 1204202, "name": "On or near Owens Way" }, "longitude": "-1.216236" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647776, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.726360", "street": { "id": 1203426, "name": "On or near Court Place Gardens" }, "longitude": "-1.237783" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647072, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.722292", "street": { "id": 1203460, "name": "On or near Spring Lane" }, "longitude": "-1.211242" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646796, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.734236", "street": { "id": 1204200, "name": "On or near Oxford Road" }, "longitude": "-1.211889" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647589, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.731954", "street": { "id": 1204211, "name": "On or near Knolles Road" }, "longitude": "-1.213463" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647367, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.727149", "street": { "id": 1203509, "name": "On or near Barns Road" }, "longitude": "-1.210275" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647146, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.726360", "street": { "id": 1203426, "name": "On or near Court Place Gardens" }, "longitude": "-1.237783" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647070, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.719306", "street": { "id": 1203470, "name": "On or near Priory Road" }, "longitude": "-1.215246" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646560, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.724426", "street": { "id": 1203436, "name": "On or near Asquith Road" }, "longitude": "-1.226653" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102646945, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.727149", "street": { "id": 1203509, "name": "On or near Barns Road" }, "longitude": "-1.210275" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647143, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.727149", "street": { "id": 1203509, "name": "On or near Barns Road" }, "longitude": "-1.210275" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647142, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.732969", "street": { "id": 1204378, "name": "On or near Sports\/recreation Area" }, "longitude": "-1.250385" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647439, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.732987", "street": { "id": 1204240, "name": "On or near Petrol Station" }, "longitude": "-1.214676" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647449, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.731954", "street": { "id": 1204211, "name": "On or near Knolles Road" }, "longitude": "-1.213463" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647353, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.738885", "street": { "id": 1204288, "name": "On or near Quartermain Close" }, "longitude": "-1.233618" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647855, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.730911", "street": { "id": 1204272, "name": "On or near Egerton Road" }, "longitude": "-1.228367" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647349, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.732987", "street": { "id": 1204240, "name": "On or near Petrol Station" }, "longitude": "-1.214676" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647526, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.737124", "street": { "id": 1204153, "name": "On or near Hendred Street" }, "longitude": "-1.220151" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647743, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.730387", "street": { "id": 1204176, "name": "On or near Boswell Road" }, "longitude": "-1.211637" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647347, "location_subtype": "", "month": "2022-06"
}, {
 "category": "anti-social-behaviour", "location_type": "Force", "location": { "latitude": "51.729723", "street": { "id": 1204166, "name": "On or near Littlemore Road" }, "longitude": "-1.218715" }, "context": "", "outcome_status": null, "persistent_id": "", "id": 102647291, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.724779", "street": { "id": 1203481, "name": "On or near Long Lane" }, "longitude": "-1.217453" }, "context": "", "outcome_status": {
  "category": "Under investigation", "date": "2022-06"
 }, "persistent_id": "3db491eb02f57648df9a2af8f1132772ec6bb8f2def3f947747bf22e65885e2b", "id": 102646899, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.732294", "street": { "id": 1204236, "name": "On or near Banjo Road" }, "longitude": "-1.217150" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "f254759e21c52428736f04bbac13b4a1ef1652e593c55e8c26499c1e57532b8a", "id": 102647441, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.740796", "street": { "id": 1204143, "name": "On or near Weyman Terrace" }, "longitude": "-1.222000" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "93c63312fb6b04b1f5c2147f0957f90506db1efdf911cc4dd0dedb1ba779c1c7", "id": 102647926, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.737804", "street": { "id": 1204239, "name": "On or near Agnes Court" }, "longitude": "-1.218271" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "6da017672b1e62fdd1e9fe7a3c8f8290554cb1c2197f1d5a32ec3370a2c64b7b", "id": 102647784, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.735523", "street": { "id": 1204199, "name": "On or near Oxford Road" }, "longitude": "-1.215965" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "b8087f61f3ee54a434603fd413351c3786696945f8311ab8fe66c1b3ce1f309b", "id": 102647657, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.735840", "street": { "id": 1204256, "name": "On or near Weirs Lane" }, "longitude": "-1.241070" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "81eb687f2a3e6f7c4bc33e23ba532dc8012ea08594a286a00d45935909dfb9ff", "id": 102647655, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.736008", "street": { "id": 1204151, "name": "On or near Littlehay Road" }, "longitude": "-1.220069" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "4d82424f72981ac1e5e3c9c77d054aa533bc1a96e8239894e3a387d1941327ce", "id": 102647644, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.734466", "street": { "id": 1204251, "name": "On or near Police Station" }, "longitude": "-1.213898" }, "context": "", "outcome_status": {
  "category": "Under investigation", "date": "2022-06"
 }, "persistent_id": "823cc48203d0ee98a4c3804fbb800f38a1b52f6a0812699860cfc5a273478fe9", "id": 102647619, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.734902", "street": { "id": 1204235, "name": "On or near Bennett Crescent" }, "longitude": "-1.210617" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "9272b4253caac3c49771bab03aa16ac377fc257b266f2624caa4979f20175973", "id": 102647599, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.734236", "street": { "id": 1204200, "name": "On or near Oxford Road" }, "longitude": "-1.211889" }, "context": "", "outcome_status": {
  "category": "Unable to prosecute suspect", "date": "2022-06"
 }, "persistent_id": "d821df571e79d0ee6c1655a57aebab9cec661651812b8e33ed0b7b729283735a", "id": 102647562, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.733850", "street": { "id": 1204221, "name": "On or near Gerard Place" }, "longitude": "-1.214632" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "08b1894f4389bfe50ccb19e1211b37a35bb201038f3e8de6f1ccff2ff651ab46", "id": 102647549, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.732987", "street": { "id": 1204240, "name": "On or near Petrol Station" }, "longitude": "-1.214676" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "350b18d189f77795e44f6881c2a1d7758c6756c2a0e1bb692d823cff882b0458", "id": 102647525, "location_subtype": "", "month": "2022-06"
}, {
 "category": "bicycle-theft", "location_type": "Force", "location": { "latitude": "51.733223", "street": { "id": 1204183, "name": "On or near Parking Area" }, "longitude": "-1.217713" }, "context": "", "outcome_status": {
  "category": "Investigation complete; no suspect identified", "date": "2022-06"
 }, "persistent_id": "45b956b4552c06ac28c56cb10ba56b13515e97fff894a9072f548469117091ab", "id": 102647520, "location_subtype": "", "month": "2022-06"
}];


/* crimes at specific street */
function getAmountOfCrimesAtStreetId(streetId) {
 const crimesAtStreetId = allCrimes.filter(crimeData => crimeData.location.street.id === streetId);
 return crimesAtStreetId.length;
}
const amountOfCrimesAtStreet_1204183 = getAmountOfCrimesAtStreetId(1204183);
console.log(amountOfCrimesAtStreet_1204183); // 4


/* crimes on each street */
// get all unique street ids
const allUniqueStreetIds = [...new Set(allCrimes.map(crimeData=>crimeData.location.street.id))];
// create an object to store amounts of crimes on each street
const amountOfCrimesAtStreet = {};
allUniqueStreetIds.forEach(streetId=>{
 const streetIdString = String(streetId);
 amountOfCrimesAtStreet[streetIdString] = 0;
});
// fill this object with amounts of crimes (incrementing inside the loop)
allCrimes.forEach(crimeData=>{
 const streetIdString = String(crimeData.location.street.id);
 amountOfCrimesAtStreet[streetIdString] =1;
});
// the same result
console.log(amountOfCrimesAtStreet["1204183"]); // 4

CodePudding user response:

Heres one example of doing it:

fetch(
  "https://data.police.uk/api/crimes-street/all-crime?lat=51.728395&lng=-1.2314066"
).then(async (data) => {
  const records = await data.json();

  // Get all street names
  const allStreets = records.map((x) => x.location.street.name);
  // if you would prefer using ids instead of street names (probably better idea), just change used object property
  // const allStreets = records.map((x) => x.location.street.id);

  // // Use reduce to create object with street name and count in format streetName:Count
  const streetsWithCount = allStreets.reduce((accumulator, value) => {
    return { ...accumulator, [value]: (accumulator[value] || 0)   1 };
  }, {});

  // if you need to sort them
  const sortableStreetNames = Object.entries(streetsWithCount);
  const sortedStreetsByCountInDescendingOrder = sortableStreetNames.sort(
    ([, a], [, b]) => b - a
  );

  console.log("streetsWithCount", streetsWithCount);
  // => {On or near Elm Tree Close: 5, On or near Champion Way: 2}
  console.log("allStreets", allStreets);
  // => ["On or near Elm Tree Close", "On or near Sports/recreation Area", "On or near Champion Way", "On or near Oxford Road", "On or near Parking Area", "On or near Pedestrian Subway", "On or near Newman Road", "On or near Constance Norman Way", "On or near Catherine Street", "On or near Littlehay Road", …]
  console.log("sortedStreetsByCount", sortedStreetsByCountInDescendingOrder);
  // => [["On or near Parking Area", 49], ["On or near Supermarket", 29]]
});


It is a pure data from an API, which is not ideal "on or near Parking Area", you didn't specify format, but it is a good place to start, maybe API has some better quering options, maybe you could just filter out some results, or format returned results in a way you want to display them.

CodePudding user response:

You can use Array#reduce to count the occurences given the street ID:

// return a promise to simplify everything
async function streetCount(streetId) {
  let response = await fetch("https://data.police.uk/api/crimes-street/all-crime?lat=51.728395&lng=-1.2314066");
  let data = await response.json();
  // create a counter
  return data.reduce((prev, cur) =>
    prev    (cur?.location?.street?.id === streetId), 0); // add the previous count to whether the street ID matches the provided one
    // we cast the boolean to a number, so if it's true it'll give 1 (add 1 to the counter) and otherwise 0
    // optional chaining is used in case the API provides malformed data; code will still work if removed
}

streetCount(1203542).then(count => {
  console.log("Crime occurences:", count);
});

  • Related