This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
So I get a JSON response which looks like this:
{
"batchcomplete": "",
"query": {
"pages": {
"97646": {
"pageid": 97646,
"ns": 0,
"title": "Die Hard",
"extract": "Die Hard is a 1988 American action film directed by John McTiernan and written by Steven E. de Souza and Jeb Stuart. It follows off-duty New York City Police Department officer John McClane (Bruce Willis) as he takes on a group of highly organized criminals led by Hans Gruber (Alan Rickman), who perform a heist in a Los Angeles skyscraper under the guise of a terrorist attack using hostages, including McClane's wife Holly (Bonnie Bedelia), to keep the police at bay.\nIt is based on Roderick Thorp's 1979 novel Nothing Lasts Forever, the sequel to 1966's The Detective, which was adapted into a 1968 film of the same name that starred Frank Sinatra. Fox was therefore contractually obligated to offer Sinatra the lead role in Die Hard, but he turned it down. The studio then pitched the film to Arnold Schwarzenegger as a sequel to his 1985 action film Commando; he turned it down, as well, and the studio finally and reluctantly gave it to Willis, then known primarily as a comedic television actor.\nMade for $28 million, Die Hard grossed over $140 million theatrically worldwide, and was given a positive reception from critics. The film turned Willis into an action star, became a metonym for an action film in which a lone hero fights overwhelming odds, and has been named one of the best action movies ever made. The film also ranks #29 on Empire magazine's 2008 list of the 500 greatest movies of all time. The film's success spawned the Die Hard franchise, which includes four sequels (Die Hard 2, Die Hard with a Vengeance, Live Free or Die Hard and A Good Day to Die Hard), video games, and a comic book."
}
}
}
}
Here is the link: https://en.wikipedia.org/w/api.php?format=jsonfm&action=query&prop=extracts&exintro=&explaintext=&titles=Die%20Hard
And I want to just get the extract of the page. The trouble is I don't know the pageid until I get the response, and so cannot call the extract.
To get the extract for this example I would have to do:
var data = JSON.parse(this.response);
console.log(suggestionData.query.pages.97646.extract);
So I have tried to get the value of the page id by making it a variable to add into the query like this:
var data = JSON.parse(this.response);
var pageid = [suggestionData.query.pages[0];
console.log(suggestionData.query.pages. + pageid +.extract);
But this hasn't been successeful as the variable pageid comes up as:
"97646": {
"pageid": 97646,
"ns": 0,
"title": "Die Hard",
"extract": "Die Hard is a 1988 American action film directed by John McTiernan and written by Steven E. de Souza and Jeb Stuart. It follows off-duty New York City Police Department officer John McClane (Bruce Willis) as he takes on a group of highly organized criminals led by Hans Gruber (Alan Rickman), who perform a heist in a Los Angeles skyscraper under the guise of a terrorist attack using hostages, including McClane's wife Holly (Bonnie Bedelia), to keep the police at bay.\nIt is based on Roderick Thorp's 1979 novel Nothing Lasts Forever, the sequel to 1966's The Detective, which was adapted into a 1968 film of the same name that starred Frank Sinatra. Fox was therefore contractually obligated to offer Sinatra the lead role in Die Hard, but he turned it down. The studio then pitched the film to Arnold Schwarzenegger as a sequel to his 1985 action film Commando; he turned it down, as well, and the studio finally and reluctantly gave it to Willis, then known primarily as a comedic television actor.\nMade for $28 million, Die Hard grossed over $140 million theatrically worldwide, and was given a positive reception from critics. The film turned Willis into an action star, became a metonym for an action film in which a lone hero fights overwhelming odds, and has been named one of the best action movies ever made. The film also ranks #29 on Empire magazine's 2008 list of the 500 greatest movies of all time. The film's success spawned the Die Hard franchise, which includes four sequels (Die Hard 2, Die Hard with a Vengeance, Live Free or Die Hard and A Good Day to Die Hard), video games, and a comic book."
}
I also want to use javascript and not jQuery etc.
Try this fiddle : https://jsfiddle.net/u9dnm70b/
var response = '{"batchcomplete": "","query": {"pages": {"97646": {"extract": "abcd" }}}}';
var suggestionData = JSON.parse(response);
var pageid = Object.keys(suggestionData.query.pages)[0];
console.log(suggestionData.query.pages[pageid].extract);
You can use Object.keys(jsonObj); to find out keys.
In your case var pageid is a json object. To access value, take a look at this thread (get keys of json-object in JavaScript)
In short answer is
for key in pageid
content= pageid[key]
extract = content.extract
You can extract pageid like this:
var pageid = Object.keys(suggestionData.query.pages)[0];
Then this should work:
console.log(suggestionData.query.pages[pageid].extract");
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 know this question was asked many times, but I believe my setup is different and so a new question needed to be asked with a different scenario)
There are plenty of examples out there that shows how to Render HTML, but I can't seem to get this to work with any of the examples. I'd like to render html the {{aboutlongs[0].description}} (this has <br /> tags that I would like to render as html)
Here's the js:
App.controller('aboutLongCtrl', function ($scope, $http) {
$http.get('test_data/ar_org.json')
.then(function (res) {
$scope.aboutlongs = res.data.aboutlong;
});
});
the HTML:
<div class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
<h6><b>About {{aboutlongs[0].name}}</b></h6>
<div class="reason-content" >
{{aboutlongs[0].description}}
</div>
</div>
Can anyone point me to the right direction?
The Json file:
"aboutlong": [{
"name": "Women's March",
"description": "The rhetoric of the past election cycle has insulted, demonized, and threatened many of us - immigrants of all statuses, Muslims and those of diverse religious faiths, people who identify as LGBTQIA, Native people, Black and Brown people, people with disabilities, survivors of sexual assault - and our communities are hurting and scared. We are confronted with the question of how to move forward in the face of national and international concern and fear.<br /><br />In the spirit of democracy and honoring the champions of human rights, dignity, and justice who have come before us, we join in diversity to show our presence in numbers too great to ignore. The Women's March on Washington will send a bold message to our new government on their first day in office, and to the world that women's rights are human rights. We stand together, recognizing that defending the most marginalized among us is defending all of us.<br /><br />We support the advocacy and resistance movements that reflect our multiple and intersecting identities. We call on all defenders of human rights to join us. This march is the first step towards unifying our communities, grounded in new relationships, to create change from the grassroots level up. We will not rest until women have parity and equity at all levels of leadership in society. We work peacefully while recognizing there is no true peace without justice and equity for all.<br /><br />Women's rights are human rights, regardless of a woman's race, ethnicity, religion, immigration status, sexual identity, gender expression, economic status, age or disability. We practice empathy with the intent to learn about the intersecting identities of each other. We will suspend our first judgement and do our best to lead without ego."
}]
Posts I've tried:
How to render a HTML tag from json value using angularJs
Angular.js How to render a HTML tag from json file
AngularJS : Insert HTML into view
if you want to render string to html i recommand to use $sce.trustAsHtml(html).
you can create a sample filter like this
.filter('trustHtml',function($sce){
return function(html){
return $sce.trustAsHtml(html)
}
})
call the filter like this inside ng-bind-html
<div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" ></div>
Demo
angular.module("app",[])
.controller("ctrl",function($scope){
$scope.aboutlongs = [{
"name": "Women's March",
"description": "The rhetoric of the past election cycle has insulted, demonized, and threatened many of us - immigrants of all statuses, Muslims and those of diverse religious faiths, people who identify as LGBTQIA, Native people, Black and Brown people, people with disabilities, survivors of sexual assault - and our communities are hurting and scared. We are confronted with the question of how to move forward in the face of national and international concern and fear.<br /><br />In the spirit of democracy and honoring the champions of human rights, dignity, and justice who have come before us, we join in diversity to show our presence in numbers too great to ignore. The Women's March on Washington will send a bold message to our new government on their first day in office, and to the world that women's rights are human rights. We stand together, recognizing that defending the most marginalized among us is defending all of us.<br /><br />We support the advocacy and resistance movements that reflect our multiple and intersecting identities. We call on all defenders of human rights to join us. This march is the first step towards unifying our communities, grounded in new relationships, to create change from the grassroots level up. We will not rest until women have parity and equity at all levels of leadership in society. We work peacefully while recognizing there is no true peace without justice and equity for all.<br /><br />Women's rights are human rights, regardless of a woman's race, ethnicity, religion, immigration status, sexual identity, gender expression, economic status, age or disability. We practice empathy with the intent to learn about the intersecting identities of each other. We will suspend our first judgement and do our best to lead without ego."
}]
})
.filter('trustHtml',function($sce){
return function(html){
return $sce.trustAsHtml(html)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<div class="background-white p20 reasons" >
<h6><b>About {{aboutlongs[0].name}}</b></h6>
<div class="reason-content" ng-bind-html="aboutlongs[0].description | trustHtml" >
</div>
</div>
</div>
Have a look at this awesome article Angular Trust Filter
angular.module('myApp', [])
.controller('aboutLongCtrl', ['$scope', '$sce', function($scope, $sce) {
$scope.aboutlongs = [{
"name": "Women's March",
"description": $sce.trustAsHtml("The rhetoric of the past election cycle has insulted, demonized, and threatened many of us - immigrants of all statuses, Muslims and those of diverse religious faiths, people who identify as LGBTQIA, Native people, Black and Brown people, people with disabilities, survivors of sexual assault - and our communities are hurting and scared. We are confronted with the question of how to move forward in the face of national and international concern and fear.<br /><br />In the spirit of democracy and honoring the champions of human rights, dignity, and justice who have come before us, we join in diversity to show our presence in numbers too great to ignore. The Women's March on Washington will send a bold message to our new government on their first day in office, and to the world that women's rights are human rights. We stand together, recognizing that defending the most marginalized among us is defending all of us.<br /><br />We support the advocacy and resistance movements that reflect our multiple and intersecting identities. We call on all defenders of human rights to join us. This march is the first step towards unifying our communities, grounded in new relationships, to create change from the grassroots level up. We will not rest until women have parity and equity at all levels of leadership in society. We work peacefully while recognizing there is no true peace without justice and equity for all.<br /><br />Women's rights are human rights, regardless of a woman's race, ethnicity, religion, immigration status, sexual identity, gender expression, economic status, age or disability. We practice empathy with the intent to learn about the intersecting identities of each other. We will suspend our first judgement and do our best to lead without ego.")
}];
}])
;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" class="background-white p20 reasons" ng-controller="aboutLongCtrl" >
<h6><b>About {{aboutlongs[0].name}}</b></h6>
<div class="reason-content" ng-bind-html="aboutlongs[0].description">
</div>
</div>
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.
I have an array like
var schedule = [
{date:'9/21/2011',mId:1,title:'Faith, Fraud and Minimum Wage',director:'George Mihalka',startTime:'9:20 PM',length:'91',genre:'Drama',movieType:'Faith, Fraud and Minimum Wage',trailer:'iVs5VL2kH8s',synopsis:'Halifax actor, dramatist and screenwriter Josh MacDonald’s hit play <i>Halo</i> has finally hit the big screen, courtesy of <i>My Bloody Valentine</i> director George Mihalka and legendary producer Colin Neale.\nA tragicomic tale of faith in a skeptical age, <i>Faith, Fraud and Minimum Wage</i> is based on real-life incidents in Nova Scotia that generated international headlines. In the economically depressed town of Nately, a young atheist maverick named Casey discovers the image of Jesus on the outside wall of her coffee-shop workplace. As outside interest builds and Nately finally makes it on the map, Casey’s own strained domestic situation boils to a climax.\nClever, moving and surprisingly respectful, <i>Faith, Fraud and Minimum Wage</i> features sparkling performances from the likes of Picnicface’s Andrew Bush (as the town priest), Callum Keith Rennie (as the dad) and Martha MacIsaac in the central role of Casey.',location:'Parklane',movieTime:'Fri, Sep 3, 7PM',moviePic:'../movies/faithfraudminimumwage/1.png'},
{date:'9/21/2011',mId:2,title:'Mind The Gap',director:'Aaron Au',startTime:'7:00 PM',length:'7',genre:'Drama',movieType:'CBC Atlantic Shorts Gala',trailer:'hbLgszfXTAY',synopsis:'Halifax actor, dramatist and screenwriter Josh MacDonald’s hit play <i>Halo</i> has finally hit the big screen, courtesy of <i>My Bloody Valentine</i> director George Mihalka and legendary producer Colin Neale.\nA tragicomic tale of faith in a skeptical age, <i>Faith, Fraud and Minimum Wage</i> is based on real-life incidents in Nova Scotia that generated international headlines. In the economically depressed town of Nately, a young atheist maverick named Casey discovers the image of Jesus on the outside wall of her coffee-shop workplace. As outside interest builds and Nately finally makes it on the map, Casey’s own strained domestic situation boils to a climax.\nClever, moving and surprisingly respectful, <i>Faith, Fraud and Minimum Wage</i> features sparkling performances from the likes of Picnicface’s Andrew Bush (as the town priest), Callum Keith Rennie (as the dad) and Martha MacIsaac in the central role of Casey.',location:'Oxford Theatre',movieTime:'Fri, Sep 8, 7PM',moviePic:'../movies/faithfraudminimumwage/1.png'},
{date:'9/24/2011',mId:2,title:'Mind The Gap',director:'Aaron Au',startTime:'7:00 PM',length:'7',genre:'Drama',movieType:'CBC Atlantic Shorts Gala',trailer:'hbLgszfXTAY',synopsis:'Halifax actor, dramatist and screenwriter Josh MacDonald’s hit play <i>Halo</i> has finally hit the big screen, courtesy of <i>My Bloody Valentine</i> director George Mihalka and legendary producer Colin Neale.\nA tragicomic tale of faith in a skeptical age, <i>Faith, Fraud and Minimum Wage</i> is based on real-life incidents in Nova Scotia that generated international headlines. In the economically depressed town of Nately, a young atheist maverick named Casey discovers the image of Jesus on the outside wall of her coffee-shop workplace. As outside interest builds and Nately finally makes it on the map, Casey’s own strained domestic situation boils to a climax.\nClever, moving and surprisingly respectful, <i>Faith, Fraud and Minimum Wage</i> features sparkling performances from the likes of Picnicface’s Andrew Bush (as the town priest), Callum Keith Rennie (as the dad) and Martha MacIsaac in the central role of Casey.',location:'Parklane',movieTime:'Fri, Sep 12, 7PM',moviePic:'../movies/faithfraudminimumwage/1.png'},
];
and I have a movie listing, and I want to see if the current movie listings mId is equal to any other mId's in the array schedule, if so return them in an array so I can loop through them and display the other results.
I am a bit confused with how to do this? I assumed you use array.filter but I can't seem to figure it out.
Any help?
array.filter should work. Try the following code:
var filtered = schedule.filter(function(val) {
return val['mId'] != searchMid;
});
where searchMid is the mid you are searching for.