I have a json data from API like below
{
"message": "success",
"data": [
{
"id": 1,
"title": "SIPHON",
"description": "<p><span style=\"color: #555555; font-family: Avenir-Regular; background-color: #ffffff;\">Siphon coffee was invented in the 1840s more or less simultaneously by a French housewife and Scottish marine engineer. It’s been refined many times, but a few principles hold true: It produces a delicate, tea-like cup of coffee; it can be quite persnickety; and it is, for our money, one of the coolest brew methods available.</span></p>\r\n<p><span style=\"color: #555555; font-family: Avenir-Regular; background-color: #ffffff;\">Sumber : <a title=\"https://bluebottlecoffee.com/preparation-guides/siphon\" href=\"https://bluebottlecoffee.com/preparation-guides/siphon\" target=\"_blank\" rel=\"noopener\">https://bluebottlecoffee.com/preparation-guides/siphon</a></span></p>",
"slug": "siphon",
"image": "4cf25879ab69702dbe12c244f233f5b4_1551366184.jpg",
"time": "{\"time1\":\"2\",\"time2\":\"3\",\"unit\":\"minute\"}",
"temperature": "{\"temperature\":\"195\",\"unit\":\"\\u00b0F\"}",
"ingredients": "[{\"name\":\"Coffee\",\"amount\":\"25\",\"unit\":\"gr\"},{\"name\":\"Hot Water\",\"amount\":\"300\",\"unit\":\"ml\"}]",
"tools": "[{\"name\":\"Grinder\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Scale\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Siphon set\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Thermometer\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Timer\",\"amount\":\"1\",\"unit\":\"unit\"},{\"name\":\"Bamboo paddle\",\"amount\":\"1\",\"unit\":\"unit\"}]",
"steps": "[\"After soaking your filter in a warm water bath for at least five minutes, drop it into the bottom of your siphon\\u2019s top component, or \\u201chopper,\\u201d and hook to the bottom of the hopper\\u2019s glass tubing.\",\"Fill your siphon\\u2019s bottom component, or \\u201cbulb,\\u201d with 300 grams of hot water (about a 12-oz. cup\\u2019s worth).\",\"Insert the hopper, filter and all, into the bulb. You don't have to press too hard; just make sure it's securely and evenly in place. Position the entire assembly above your heat source.\",\"While the water is heating, measure out between 20-25 grams of coffee and grind it just little bit finer than you would for regular drip coffee.\",\"Soon, the water in the bulb will begin boiling and rise up into the hopper. For some physics-related reason we don\\u2019t fully understand, a little bit will stay in the bottom. Don\\u2019t worry about this little bit.\",\"Once the water has moved into the hopper, turn your heat source down so that the water is between 185-195 degrees F.\",\"Add your coffee, and gently (but thoroughly) submerge it with a bamboo paddle or butter knife.\",\"Let the coffee brew, undisturbed, for one minute and 10 seconds.\",\"In one brisk motion, remove your siphon from its heat source and give it ten stirs with a bamboo paddle.\",\"Your coffee should take another minute or so to draw downward and finally rest in the bulb. You'll know it's ready when a dome of grounds has formed at the top of the filter, and when the coffee at the bottom has begun to bubble at approximately the pace and strength of a kitten\\u2019s heartbeat.\\r\\n\\r\\nRemove the hopper and serve. In order to guarantee the most complex cup, give the coffee a few minutes to cool.\"]",
"step_images": "[\"4cf25879ab69702dbe12c244f233f5b4_11551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_21551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_31551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_41551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_51551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_61551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_71551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_81551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_91551366184.jpg\",\"4cf25879ab69702dbe12c244f233f5b4_101551366184.jpg\"]",
"user_id": 1,
"status_id": 1,
"shared_id": 1,
"created_at": "2019-02-28 15:03:04",
"updated_at": "2019-02-28 15:03:04",
"deleted_at": null
}
]
}
What I want to do is split string in steps and store it in array. The delimiter is .\", . Thanks.
You have to first parse the JSON by:
let obj = JSON.parse(yourJsonStringObject)
after that,
let arrayOfSteps = obj.data[0].steps.split(".\")
steps is string so you can parse your output like this:
Let's say if you have stored the above json in variable apiResponse;
Then get the parsed string:
var parsedResponse = JSON.parse(apiResponse["data"][0]["steps"]);
Related
So I have a list of movies stored with a database and when I make a request to the endpoint the data is returned as such
**{
"Actors": [],
"_id": "613345b5c3e971923de94876",
"Title": "Gladiator",
"Description": "A former Roman General sets out to exact vengeance against the corrupt emperor who murdered his family and sent him into slavery.",
"ImagePath": "https://m.media-amazon.com/images/I/51m50jXZsUL._AC_.jpg",
"Featured": true,
"Genre": [
{
"_id": "61373bf26af4f5693de54a3e",
"Name": "Action",
"Description": "Action films involve one or more heroes thrust into a series of challenges requiring physical feats, extended fights, extensive stunts and frenetic chases.Story and character development are generally secondary to explosions, fist fights, gunplay and car chases. Both historically and currently, action films have wide commercial appeal and enjoy box office success. The action film revolves around a narrative, to be sure, but more importantly than that, a hero; when a moviegoer thinks of an action picture, more often than not they are thinking of a specific actor and the obstacles their character(s) must overcome."
}
],
"Director": [
{
"_id": "613741376af4f5693de54a41",
"Name": "Ridley Scott",
"Bio": "Ridley Scott is an English film producer and director, regarded as one of the biggest names in Hollywood. Ridley is known for his science-fiction horror films, such as the cult classic ‘Alien’ and the science-fiction existential drama ‘Blade Runner.’ Belonging to an army family, Scott was in love with films since childhood. He began his career with short films and eventually made his first feature film, ‘The Duellists,’ in 1977. The film won the ‘Best Debut Film’ honor at the ‘Cannes Film Festival.’ However, it was his second feature film ‘Alien’ that introduced him to Hollywood in a grand style. His highly moody and atmospheric style of directing was fairly new to mainstream Hollywood audiences. Scott quickly sealed his place as one of the most prominent young directors. Scott repeated his mainstream success with ‘Blade Runner.’ Over the years, with the success of films such as ‘Kingdom of Heaven,’ ‘Robin Hood,’ and ‘Black Hawk Down,’ he became a name to reckon with. Of late, he has garnered appreciation for films such as ‘Gladiator,’ ‘The Martian,’ ‘Prometheus,’ and ‘Alien: Covenant.’ He has earned multiple nominations for prestigious awards such as the ‘Emmy Award,’ the ‘Academy Award,’ and the ‘Golden Globe Award.’ He has won two ‘Emmies’ too",
"Birth": "1937",
"Death": ""
}
]
}**
When I try to retrieve this information in another view
**<div className = "movie-director">
<span className = "label">Director: </span>
<span className = "value">{movie.Director.Name}</span>**
All I get is the ID of the director. I have tried to access the array using Array[].name but can't seem to get anything to display. Any suggestions?
I am building a news website which gets data from an external server. The external server often sends me the links to particular tweets but I can't seem to find a way to convert them into an embedded tweet
(Please note stack overflow wont let me send shortened urls, therefore it is in quotations.)
The json that it sends me is like -
"Story": "ROME: Harry Kane scored twice as a buoyant England cruised through to the semi-finals of Euro 2020 with a one-sided 4-0 win over Ukraine in Rome on Saturday. Kane ended a worrying international scoring drought by netting in the 2-0 last-16 defeat of Germany in midweek and he put England ahead inside four minutes on a sweaty evening in the Italian capital. <strong>As it happened: Ukraine vs England</strong> Gareth Southgate's side then put this quarter-final tie out of sight with two more goals early in the second half, one from Harry Maguire before Kane netted again. Substitute Jordan Henderson got the fourth, and as Denmark lie in wait in the Wembley semi-final on Tuesday England will be confident of going on to reach a first ever European Championship final and even now claiming a first major international title since 1966. <p>�������������� England = semi-finalists ������#EURO2020 | #ENG https://twitter.com/EURO2020/status/1411427976047120387"</p>— UEFA EURO 2020 (#EURO2020) 1625346114000 The draw here was kind for them, with Ukraine surely as weak an opponent as they could hope to face in a quarter-final, a stage at which they have lost to the likes of Italy and Portugal in recent European Championships. However the statistics are impressive, with England having come through five games at this tournament all without conceding a goal. Some of their play in wide areas was outstanding, with Raheem Sterling and Jadon Sancho -- making his first start at the Euro -- too hot for Ukraine to handle. <p>⏰ RESULT ⏰What. A. Performance. �������������� Kane (2), Maguire & Henderson net in Rome as England reach EURO 2020 se… https://twitter.com/EURO2020/status/1411427976047120387"</p>— UEFA EURO 2020 (#EURO2020) 1625345627000 Kane, their captain, had gone close to eight hours without finding the net for his country but his opener here was his second in just eight minutes following the late strike that secured victory over Germany. Regardless of the opposition, their display at the Stadio Olimpico was a step-up in class in the final third to previous games at the Euro and they will be favourites at home against a Danish side who played their own quarter-final against the Czech Republic on Saturday in distant Baku. This will be the only match England play away from home in the competition and it marked quite a difference to their defeat of the Germans, which was watched by more than 40,000 supporters at Wembley, where coronavirus restrictions were eased. <p>�������������� Two-goal England hero Harry Kane takes the plaudits after inspiring the Three Lions in Rome ��#Heineken |… https://twitter.com/EURO2020/status/1411427976047120387"</p>— UEFA EURO 2020 (#EURO2020) 1625346478000 With Italy currently imposing a five-day quarantine on all arrivals from the United Kingdom, the number of England fans in Rome was limited to those already based in the European Union although they still made themselves heard in the crowd of under 12,000. They had plenty to celebrate, unlike their Ukrainian counterparts, as Andriy Shevchenko's team came up short in their bid to take the country to a first ever major tournament semi-final. They scraped out of their group and then edged 10-man Sweden in extra time in the last 16, and their chances of shocking England looked dead and buried when they fell behind early on. Sterling, who terrorised the Ukraine defence down the left, played in Kane who poked the ball past Georgiy Bushchan. Ukraine's giant striker Roman Yaremchuk forced a save from Jordan Pickford and a Declan Rice piledriver was kept out by Bushchan, with England looking comfortable. However Ukraine were a different proposition after injured defender Serhiy Kryvtsov was replaced by Dynamo Kiev winger Viktor Tsygankov in the 36th minute. They finished the first half strongly and more pessimistic England fans may have spent the interval reliving their exit from Euro 2016, when they lost to Iceland in the last 16 despite also having opened the scoring in the fourth minute. They need not have worried. England scored again less than a minute after the restart when a foul on Kane allowed Luke Shaw to deliver a free-kick from the left for Maguire to head in. Four minutes after that Sterling supplied the overlapping Shaw and he crossed for a rejuvenated Kane to head home. The Tottenham star nearly had his hat-trick, a stinging volley producing a fine save from Bushchan. From Mason Mount's resulting corner came the fourth goal, another header, this time from Henderson, the first of five substitutes sent on by Southgate who would have been thinking about the semi-final long before this quarter-final was officially over. ",
Edit: Based on the reply below this. Let me clarify My server sends me data in the form of json objects with strings in it. I pass this data using ejs tags to their appropriate location to get rendered. I am asking how can i convert every twitter url that winds up in my json object to an embedded tweet.
Edit: This is the json that i received from the server I can render that just fine on my webpage but i cant seem to find a way to convert the hyperlinks to embeded tweet
just add "https://publish.twitter.com/oembed?" before your url as mentioned here.
you can also customize the embedded tweet as described in the link.
the response would be something like
{
"url": "https://twitter.com/Interior/status/507185938620219395",
"author_name": "US Dept of Interior",
"author_url": "https://twitter.com/Interior",
"html": "<blockquote class="twitter-tweet"><p lang="en" dir="ltr">Happy 50th anniversary to the Wilderness Act! Here's a great wilderness photo from #YosemiteNPS. #Wilderness50 pic.twitter.com/HMhbyTg18X</p>— US Dept of Interior (#Interior) September 3, 2014</blockquote>n<script async src="//platform.twitter.com/widgets.js" charset="utf-8"></script>",
"width": 550,
"height": null,
"type": "rich",
"cache_age": "3153600000",
"provider_name": "Twitter",
"provider_url": "https://twitter.com",
"version": "1.0"
}
and you can use the html property as embed tweet.
I think this should work for you. Twitter provides an api for creating embed html from a tweet url.
const axios = require('axios');
const main = async () => {
const tweetURL = 'https://twitter.com/thejackbeyer/status/1411522480775204866?s=20';
try {
const response = await axios.get(`https://publish.twitter.com/oembed?url=${tweetURL}`);
console.log(response.data.html);
} catch (error) {
console.error(error);
}
}
main();
I need to access some data from NASA's API, which is in a nested object. Their data is sorted by date, so each key is formatted like so: 2018-09-07
const asteroidList = this.props.asteroids.near_earth_objects //works fine, can access data up to this point
console.log(asteroidList)
const asteroidList = this.props.asteroids.near_earth_objects[2018-09-07] // "Legacy octo literals are not allowed in strict mode" ????
console.log(asteroidList) //errors out
I cannot access anything past this point, because I keep getting an error from my text editor. I'm assuming there must be some kind of conversion method or something to read dates that I don't know about, but I can't find anything for object key-value pairs.
I check what NASA APIs response is, and the key is not a date-value but the "date" string.
So if you have a list of objects and you want to access only the one with a specific date you have to iterate over those objects and search for that particular object:
const list = [
{
"date": "1995-06-16",
"explanation": "Today's Picture: Explanation: If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star. We keep an archive file. Astronomy Picture of the Day is brought to you by Robert Nemiroff and Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
"hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
"media_type": "image",
"service_version": "v1",
"title": "Neutron Star Earth",
"url": "https://apod.nasa.gov/apod/image/e_lens.gif"
},
{
"date": "1999-07-11",
"explanation": "Today's Picture: Explanation: If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star. We keep an archive file. Astronomy Picture of the Day is brought to you by Robert Nemiroff and Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
"hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
"media_type": "image",
"service_version": "v1",
"title": "Neutron Star Earth",
"url": "https://apod.nasa.gov/apod/image/e_lens.gif"
},
{
"date": "2010-01-22",
"explanation": "Today's Picture: Explanation: If the Earth could somehow be transformed to the ultra-high density of a neutron star , it might appear as it does in the above computer generated figure. Due to the very strong gravitational field, the neutron star distorts light from the background sky greatly. If you look closely, two images of the constellation Orion are visible. The gravity of this particular neutron star is so great that no part of the neutron star is blocked from view - light is pulled around by gravity even from the back of the neutron star. We keep an archive file. Astronomy Picture of the Day is brought to you by Robert Nemiroff and Jerry Bonnell . Original material on this page is copyrighted to Robert Nemiroff and Jerry Bonnell.",
"hdurl": "https://apod.nasa.gov/apod/image/e_lens.gif",
"media_type": "image",
"service_version": "v1",
"title": "Neutron Star Earth",
"url": "https://apod.nasa.gov/apod/image/e_lens.gif"
}
]
list.forEach(element => {
if (element.date === '1999-07-11') {
console.log(element);
}
});
You need to pass 2018-09-07 inside quotes "2018-09-07"
Also add check for undefined cases to avoid error.
const asteroidList = this.props.asteroids && this.props.asteroids.near_earth_objects && this.props.asteroids.near_earth_objects['2018-09-07']
console.log(asteroidList)
This question already has answers here:
How to strip HTML tags from string in JavaScript? [duplicate]
(4 answers)
Closed 5 years ago.
In my React Native app i'm fetching json data which contains raw html tags.How to remove the html tags from that?Following is the json response i'm getting
[{
"data": {
"course": {
"id": 2864,
"name": "2. Understanding India’s economic transition",
"date_created": 1506154480,
"status": "publish",
"price": false,
"price_html": "FREE",
"total_students": 0,
"seats": "",
"start_date": false,
"average_rating": 0,
"rating_count": 0,
"featured_image": "https://www.mywebsite.com/lms/wp-content/themes/wplms/assets/images/avatar.jpg",
"categories": [],
"instructor": {
"id": "22",
"name": "aami",
"avatar": "https://www.mywebsite.com/lms/wp-content/uploads/2017/11/favicon.png",
"sub": ""
},
"menu_order": 0
},
"description": "<p style=\"text-align: justify;\">India is undergoing an economic, social and technological transformation. Perhaps no other phase is going to be as critical as the present one in shaping the future of the country as well as determining the welfare of the people. The economic transition is the vital ingredient of these overall change. Faster growth accompanied by industrial sector expansion, skill addition to the people, creation of quality infrastructure etc. will fuel this growth phase.</p>\n<p style=\"text-align: justify;\">For understanding how important is the present development phase, we have to adopt a historical and comparative study. Following factors helps us to understand the present development phase of India.</p>\n\n<ol style=\"text-align: justify;\">\n \t<li><strong>Achievement of higher growth rate as a middle-income economy. </strong></li>\n</ol>\n<p style=\"text-align: justify;\">India at present is a lower middle-income economy and has to become a high-income economy undergoing a rapid growth phase extending at least three decades.</p>\n<p style=\"text-align: justify;\">Most important narrative about the Indian economy is that it is the third largest in the world in terms of Purchasing Power Parity GDP. But a superior way to asses a country’s development is to consider per capita income. The widely used ranking about countries’ economic position is that of the World Bank’s GDP Per capita (constant US $) and the data for 2016 shows that India’s per capita income is $1861 compared to China’s $ 6994.</p>\n<p style=\"text-align: justify;\">Table: Categorization of countries by World Bank</p>\n\n<table>\n<tbody>\n<tr>\n<td width=\"198\"><strong>Category</strong></td>\n<td width=\"318\"><strong>PCI as on 2015 in constant US Dollar</strong></td>\n</tr>\n<tr>\n<td width=\"198\">Low Income Economy</td>\n<td width=\"318\">$ 1025 or less</td>\n</tr>\n<tr>\n<td width=\"198\">Middle Income Economy\n\n(India - $ 1861)</td>\n<td width=\"318\">$1026 to $4035 (Lower Middle Income)\n\n$4036 to $12475 (Upper Middle Income)</td>\n</tr>\n<tr>\n<td width=\"198\">Higher Income Economy</td>\n<td width=\"318\">$ 12476 and above</td>\n</tr>\n</tbody>\n</table>\n<p style=\"text-align: justify;\">According to World Bank metrics, a country with less than $1045 is considered as low-income economy whereas one with a PCI of $12736 or higher is considered as a higher income economy. Higher income means higher standard of living. India is at the bottom of the lower middle-income economies and has to achieve higher economic growth, structural changes including industrialisation to raise per capita income near to the $12736 mark in the long run. This is what the country has to achieve through the transition.</p>\n\n<ol style=\"text-align: justify;\" start=\"2\">\n \t<li><strong> Industrial sector expansion</strong></li>\n</ol>\n<p style=\"text-align: justify;\">There are several factors that drives the economy to prosperity. Per capita income is just a monitored goal. How it can be raised is through achieving more productivity and employment generation in the sectors that can create big changes is decisive element for the country’s transition. Here comes the role of industrial sector. The industrial sector is known for generating huge employment with minimum skill addition. Similarly, no other sector has higher level of tradability as the industrial sector (means a country can earn big income through exports). Graduating to an expanded services sector without undergoing industrialisation will be self-defeating and unsuitable to a big economy like India. Depending on services sector for exports will not reward as other countries like to protect their services sector from the inflow of India’s skilled persons. If India can increase the contribution of the industrial sector in GDP from the present 30 per cent to say 40-45%, it implies that sizable income and employment are created in the sector.</p>\n\n<ol style=\"text-align: justify;\" start=\"3\">\n \t<li><strong> Skilling the people when demography favours.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">India has the largest number of young people in the world besides having largest workforce age group population. This situation is expected to remain till 2045. Now, youth means higher ability to produce, consume and thus stimulate overall economic activities. As in the case of an individual, better things happen for an economy when it is young.</p>\n\n<ol style=\"text-align: justify;\" start=\"4\">\n \t<li><strong> Infrastructure generation.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">Infrastructure is the platform for fueling growth. The government has launched several programmes to build quality infrastructure to assist economic transformation. In the industrial sector, there is the industrial corridor project; in transportation – there is the NHDP, PMGSY, Bhartamala, Sagarmala etc. Similarly, digital infrastructure is undergoing a qualitative improvement along with the education sector.</p>\n\n<ol style=\"text-align: justify;\" start=\"5\">\n \t<li><strong> Building invention and Innovation.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">Development is dynamic and present day developing economies can’t achieve development with the strategy of the past. Fourth industrialisation, robotics, artificial intelligence etc., proved that industrialisation is not labour oriented. Here, Countries like China with the aid of superior technology with its sizable labour force can produce and supply goods to the entire world. Competing in the new age industrial sector need good technological adaptation and a progressive national invention and educational systems.</p>\n\n<ol style=\"text-align: justify;\" start=\"6\">\n \t<li><strong> Easing of Doing Business.</strong></li>\n</ol>\n<p style=\"text-align: justify;\">India’s business environment is historically suffocated by excess regulations and slow bureaucratic functions that are unsuitable for enterprise development.. But in recent years, institutional reforms are taking place and development blocking regulations are in the process of elimination.</p>\n<p style=\"text-align: justify;\">Development has become a major theme of the government and every issue related with economic prosperity are sophisticatedly addressed. Improvements in all the above field is slowly yielding results. Despite the adverse global slowdown and anti-globalisation headwinds, India continues to be the fastest growing large economy.</p>",
"curriculum": false,
"reviews": [],
"instructors": [{
"id": "22",
"name": "tojo",
"avatar": "https://www.mywebsite.com/lms/wp-content/uploads/2017/11/favicon.png",
"sub": "",
"average_rating": 0,
"student_count": 0,
"course_count": "0",
"bio": false
}],
"purchase_link": false
},
"headers": [],
"status": 200
}]
I'm listing description inside a card. So how to remove html tags?Please do help..Is there a way to do this?
What about this?
const regex = /(<([^>]+)>)/ig;
const result = data.description.replace(regex, '');
I also faced this challenge in my app.
After trying several library, i'd like to recommand you react-native-render-html
It allows you to provide 'html' to a Native component and have a simple text as an output.
You can also customize each tag to give them specific style or ignore some of them.
It does not require you to use 'react link' to make it work so you can use it in your expo app as well.
Hope it helps.
JSFiddle
I am using Facebook's API to pull in daily crime reports from my county's police department page. They follow a mostly standardized format, with the following patterns being what I'm going off of, and a few annoying inconsistencies:
The header is between 3-4 lines followed by two new line characters \n\n (The code cuts this out and is not part of the output below)
Different categories of crimes committed are grouped together with the first line being a capitalized string describing the types of crimes. Each category is separated by two new line characters \n\n above it.
Actual crimes committed follow the category title described above, each (most of the time) separated by one new line character \n
As an "artifact" of whatever they are copying and pasting from, a few times there are various unicode characters substituting the hyphen, including \u2013, \u2014 and \u2015
All crimes reported start with the string "BEAT", or on rare occasion "Beat"
The problem that I am running into is that sometimes the code below catches a category title detailed in #2 above, yet in other posts, the (seemingly) exact same string and circumstances doesn't catch. The angular code I'm using in a service can be seen below
me.parsePosts = function() {
var posts = facebookService.getRandomPosts(); // Just a method to return 5 random reports for now
angular.forEach(posts, function(post) {
// Some reports are incorrectly double spaced and inconsistent
// with spacing and capitalization
var fixedPost = post.message
.replace(/^Beat/, 'BEAT') // They were a little inconsistent back in the day
.replace('\n\n###', '') // All posts end with a useless ###
.replace('\u2013', '-') // Pesky unicode characters!
.replace('\u2014', '-')
.replace('\u2015', '-')
.replace('\n\nARRESTED', '\nARRESTED') // would help if this was consistent
.replace(/(?:\\[rn ]|[\r\n ]+)BEAT/gi, '\nBEAT'), // same with the reports...
postSplit = fixedPost.split('\n\n'), // split up the post into potential categories
header = postSplit.splice(0,1); // I don't want the standard header of the post
// Pass in postSplit .join()'d back together for debugging
me.getCategoriesFromPost(postSplit, postSplit.join('\n\n'));
});
};
me.getCategoriesFromPost = function(postArray, post) {
var categoryRegexp = /[A-Z\-&\/: ]+$/,
categories = [], uniqCategories = [];
angular.forEach(postArray, function(a) {
var split = a.split('\n'), // Extract the category from the list of crimes
potentialCategory = split[0].trim(); // There's often an unwanted trailing space
if (potentialCategory.match(categoryRegexp)) {
categories.push(potentialCategory);
}
});
// Every blue moon they repost a category twice, I just want one
// and I'll merge the two together afterwards
uniqCategories = categories.filter(function(a,b) {
return categories.indexOf(a) == b;
});
console.log(uniqCategories); // log off all the categories in the post
console.log(post); // Display the actual post so i can visibly verify it all worked
};
So as an example, in one post:
console.log(uniqCategories); (original raw text as received from facebookService.getRandomPosts()):
BURGLARY COMMERCIAL
BEAT E1 SPRINT WIRELESS, 7300 ASSATEAGUE DR, 3/19 0426: Unknown suspect(s) gained entry to the business by breaking the glass door. The suspect(s) stole electronics. 14-25638
BEAT D6 MONTPELIER LIQUORS, 7500 MONTPELIER RD, 3/19 0513: Unknown suspect(s) gained entry to the business by breaking the glass door. The suspect(s) stole liquor, lottery tickets, and an ATM machine. 14-25641
BEAT D4 MACY’S, 10300 LITTLE PATUXENT PKWY, 3/19 0501: Two unknown male suspects, wearing masks, gained entry to the business by breaking the glass door. The suspects were interrupted by a store employee and fled without taking anything. 14-25642
SUSPECT VEHICLE: black Dodge pickup
BURGLARY NON COMMERCIAL
BEAT B3 6600 ASPERN DR, 3/17 2354: Four suspects gained entry to the residence via unknown means. No sign of forced entry. 14-25220
ARRESTED:
Karlin Lamont Harris, 23, of Pirch Way in Elkridge, charged with fourth-degree burglary
Steven Lee Hubbard, 29, of Edgewater, charged with fourth-degree burglary
Jessie Tyler Holt, 22, of Pine Tree Rd in Jessup, charged with fourth-degree burglary
Brittney Victoria McEnaney, 26, of Pasadena, charged with fourth-degree burglary
BEAT C1 6900 BENDBOUGH CT, 3/18 1400: Unknown suspect(s) gained entry to the residence via the front door. No sign of forced entry. The suspect(s) stole jewelry. 14-25392
BEAT B4 7100 DEEP FALLS WAY, 3/18 1100-1440: Unknown suspect(s) gained entry to the residence by forcing a rear basement window. The suspect(s) stole jewelry and electronics. 14-25404
VEHICLE THEFT & ATTEMPTS
BEAT E2 7-11, 9600 WASHINGTON BLVD, 3/18 0409:
05 Acura Tag 1AV8629 14-25277 (Keys left in vehicle.)
And console.log(post); returns
["BURGLARY COMMERCIAL", "BURGLARY NON COMMERCIAL", "VEHICLE THEFT & ATTEMPTS"]
Yet on another post, console.log(uniqCategories); (original raw text as received from facebookService.getRandomPosts()):
ROBBERY COMMERCIAL
BEAT B3 ZIPS DRY CLEANING, 6500 OLD WATERLOO RD, 3/22 1900: An unknown suspect entered the business through an unlocked rear door. The suspect threatened an employee and demanded cash. The employee complied. The suspect fled the business. 14-26959
SUSPECT: B/M, 5’8-5’9, black hoodie and pants, backpack
ROBBERY NON COMMERCIAL
BEAT E7 7-11 PARKING LOT, 9100 MAIER RD, 03/23 1632: Suspect stole cash from an acquaintance and caused an abrasion with an unknown sharp object. Police are investigation the possibility it may be drug related. 14-27243
SUSPECT: B/M, 5’8, 200 lbs, dreadlocks
BURGLARY COMMERCIAL
BEAT E1 MEGATELECOM, 8600 WASHINGTON BLVD #106, 3/22 0933: Unknown suspect(s) gained entry to the business by breaking a window. The suspect(s) stole electronics. 14-26793
BEAT F3 CATTAIL CREEK COUNTRY CLUB, 3600 CATTAIL CREEK DR, 03/22 1600- 03/23 0630: Unknown suspect(s) gained entry to a garage through an unlocked door. The suspect(s) stole golf carts. 14-27127
BURGLARY NON COMMERCIAL
BEAT E2 9300 BREAMORE CT, 03/21 1210 ATTEMPT: Two suspects attempted to gain entry via a rear slider. The resident yelled and the suspects fled, but were later caught by police. 14-26458
ARRESTED:
Travis Donte Mackell, 23, of Baltimore, charged with fourth-degree burglary
Maurice Debuiel Aye, 26, of Baltimore, charged with fourth-degree burglary
BEAT D3 5500 COLUMBIA RD, 3/21: An unknown suspect gained entry to the residence through an unlocked rear slider. The suspect woke the resident, who ultimately got the suspect to leave. It appears he may have entered the wrong residence. 14-26712
SUSPECT: B/M, 5’8, 200 lbs
BEAT B4 7500 HEARTHSIDE WAY, 3/22 1700- 1800: Three unknown black male suspects stole a bicycle, which was unsecured on a bike rack. 14-27185
BEAT E3 9100 BRYANT AVE, 3/23 2213: Unknown suspects gained entry to the residence by prying open the kitchen window. Nothing appeared to be taken. 14-27308
BEAT B3 8000 KEETON RD, 3/23 1930- 2230: Unknown suspect(s) gained entry to the residence through an unlocked window. The suspect(s) stole a computer and jewelry. 14-27314
BEAT A3 9000 FREDERICK RD, 3/23 0205: The suspect kicked in an acquaintance’s door after a verbal altercation and assaulted him. 14-27361
ARRESTED: Michael Wilson Sittig, 34, of Frederick Road in Ellicott City, charged with second-degree assault, third- and fourth-degree burglary, malicious destruction of property, and disorderly conduct
VEHICLE THEFT & ATTEMPTS
BEAT D2 5100 ELIOTS OAK DR, 03/22 2130- 3/23 0700:
12 Hyundai Sonata Red MD 5AN2945 14-27135
and console.log(post) only returns:
["ROBBERY COMMERCIAL", "VEHICLE THEFT & ATTEMPTS"]
I expect it to return ["ROBBERY COMMERCIAL", "ROBBERY NON COMMERCIAL", "BURGLARY COMMERCIAL", "BURGLARY NON COMMERCIAL", "VEHICLE THEFT & ATTEMPTS"]
In that instance, it's clear that my code matches the former instances of BURGLARY COMMERCIAL and BURGLARY NON COMMERCIAL, but not the latter. What gives? Also, feel free to correct me and tell me I'm doing it all wrong with the wall of .replace(), and that there's a better way to do it, if there is. Thanks a bunch for the help!
String.replace replaces the FIRST occurrence. You need to change all your String.replace with a regex to replace all occurrences. Something like this (although I'm not sure how the unicode chars work in regex):
post.message
.replace(/^Beat/ig, 'BEAT') // They were a little inconsistent back in the day
.replace('/\n\n###/g', '') // All posts end with a useless ###
.replace('/\u2013/g', '-') // Pesky unicode characters!
.replace('/\u2014/g', '-')
.replace('/\u2015/g', '-')
.replace('/\n\nARRESTED/g', '\nARRESTED') // would help if this was consistent
.replace(/(?:\\[rn ]|[\r\n ]+)BEAT/gi, '\nBEAT'), // same with the reports...
You were missing a few more delimiter replacements before your split. Namely, I added:
post.message
...
.replace( /\s*\n\s\n/g, '\n\n')
.replace(/\s BEAT/g, 'BEAT') ...
See updated fiddle
TL;DR; (updated based on comments)
If you look at the messages after the original replace(...) function calls, and before the .split('\n\n'), some of them have a blank space at the very end followed by a newline, then another blank, and newline.
None of your original replace() took care of that. Also, some only had a newline, blank, newline pattern (& why the first space in the regex has a *). Then, some of the BEAT keywords in the message were preceded by one or more blanks so we are removing those to ensure that BEAT is always preceded by a newline.
If you un-comment out the logging lines in the fiddle and comment out the fix, you will see the array of elements at each step.
In one of those, you will see that one array element contains not only what we expect (one report) but the next category is embedded there as well (which is why you would see fewer).
Then I just tried to see what was different about those line endings and checking if the replace() functions took care of them before the split(...) call...
Let me know if you want me to explain it better.