I'm fetching data from different sources and the output string looks like the following:
"addressId":"132234","businessEntryCount":2026},{"district":"Nordend-West","districtSlug":"frankfurt-am-main-nordend-west","addressId":"132232","businessEntryCount":1925}],"generated":"2022-01-23 19:35:43.469","grisuLocation":null,"district":null,"geo":null};
kt.Data.SearchResult.distanceLocation = "Frankfurt am Main";
kt.Data.SearchResult.distanceStreetnumber = "";
kt.Service.citySlug = 'frankfurt';
kt.Data.what = 'Handwerker';
kt.Data.where = 'Frankfurt am Main';
kt.Data.trade = 'Maler';
{"#context":"http:\/\/schema.org","#type":"SearchResultsPage","mainEntity":{"#type":"ItemList","itemListElement":[{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdecker Olaf Pocklitz","url":"http:\/\/www.test.de","email":"test#t-online.de","address":{"#type":"PostalAddress","postalCode":"65931","addressLocality":"Frankfurt","addressRegion":"Hessen",
The above string content is my data result. I want to have only all the email addresses left to save them in a file. In the above example it would be test#t-online.de and if we have more than one email address, then I want the second email address in a new line. Im struggling how to perfectly filter them out and afterwards save them one by line. I already made it work to save it but I don't know how to only get the email address out of it:
console.log('received data: ' + data)
fs.writeFileSync('./results/test.json', data)
EDIT:
This is my code so far:
var matches = data.match(/\"mainEntity":{"(.*?)\"}/);
var preResult = [matches]
//.itemListElement[0].item.email
console.log('received data: ' + preResult)
fs.writeFileSync('./results/test.json', preResult)
and my result but im not able to access the email:
"mainEntity":{"#type":"ItemList","itemListElement":[{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdecker Olaf test","url":"http:\/\/www.test.de","email":"test#t-online.de","address":{....
With this selector: element.mainEntity.itemListElement[0].item.email
const element = {
"#context":"http:\/\/schema.org",
"#type":"SearchResultsPage",
"mainEntity":{
"#type":"ItemList",
"itemListElement":[
{
"#type":"ListItem",
"item":{
"#type":"LocalBusiness",
"name":"Dachdecker Olaf Pocklitz",
"url":"http:\/\/www.test.de",
"email":"test#t-online.de",
"address": ""
}
}
]
}}
console.log(element.mainEntity.itemListElement[0].item.email)
Update
d = {"#context":"http:\/\/schema.org","#type":"SearchResultsPage","mainEntity":{"#type":"ItemList","itemListElement":[{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdecker Olaf Pocklitz","url":"http:\/\/www.pocklitz.de","email":"opocklitz#t-online.de","address":{"#type":"PostalAddress","postalCode":"65931","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Erfurter Weg 21"},"telephone":"(069) 765820","aggregateRating":{"#type":"AggregateRating","worstRating":1,"bestRating":5,"ratingValue":1,"reviewCount":3,"itemReviewed":{"#type":"Organization","name":"Dachdecker Olaf Pocklitz"}}}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdeckerei Havan","url":"http:\/\/www.dachdeckereihavan.de","email":"info#dachdeckereihavan.de","address":{"#type":"PostalAddress","postalCode":"60599","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Offenbacher Landstr. 364"},"telephone":"(069) 651540"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdeckerfachbetrieb Thomas Piller","email":"piller-bedachungen#gmx.de","address":{"#type":"PostalAddress","postalCode":"60439","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Eduard-Bernstein-Weg 3"},"aggregateRating":{"#type":"AggregateRating","worstRating":1,"bestRating":5,"ratingValue":5,"reviewCount":1,"itemReviewed":{"#type":"Organization","name":"Dachdeckerfachbetrieb Thomas Piller"}}}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dachdeckermeisterbetrieb Tuvana","url":"http:\/\/www.tuvana-dach.de","email":"info#tuvana-dach.de","address":{"#type":"PostalAddress","postalCode":"60596","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Tiroler Str. 28c"},"telephone":"(069) 78809681"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Decos GmbH","url":"http:\/\/www.decos-gmbh.de","email":"decosgmbh#web.de","address":{"#type":"PostalAddress","postalCode":"60388","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Flinschstr. 21"},"telephone":"(069) 42603940"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Dejan Miloradovic","email":"dmilo#web.de","address":{"#type":"PostalAddress","postalCode":"60487","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Sophienstr. 25"},"telephone":"(069) 703177"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Denstedt Bedachungs-GmbH","email":"denstedt.bedachungsgmbh#t-online.de","address":{"#type":"PostalAddress","postalCode":"60388","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Riedstr. 43"},"telephone":"(06109) 31967"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Der Zollstock - Ulrich Heuser & Hans-J\u00fcrgen Kurth","url":"https:\/\/www.der-zollstock.de","email":"kundenbetreuung#der-zollstock.de","address":{"#type":"PostalAddress","postalCode":"60388","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"R\u00f6ntgenstr. 8"},"telephone":"(06109) 378400"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Die Stadtzimmerei Stefan Scherer","url":"http:\/\/www.die-stadtzimmerei.de","email":"info#die-stadtzimmerei.de","address":{"#type":"PostalAddress","postalCode":"60385","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Wittelsbacherallee 102"},"telephone":"(069) 43059517"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"einfeldt Baudekoration GmbH","url":"https:\/\/www.einfeldt-baudeko.de","email":"info#einfeldt-baudeko.de","address":{"#type":"PostalAddress","postalCode":"60386","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Orber Str. 42"},"telephone":"(06039) 3878"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Emil Scholz GmbH & Co. KG","url":"https:\/\/www.malerteam-scholz.de","email":"info#malerteam-scholz.de","address":{"#type":"PostalAddress","postalCode":"60385","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Ostparkstr. 55"},"telephone":"(069) 943411-0","aggregateRating":{"#type":"AggregateRating","worstRating":1,"bestRating":5,"ratingValue":1,"reviewCount":1,"itemReviewed":{"#type":"Organization","name":"Emil Scholz GmbH & Co. KG"}}}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Ewald Thamer","email":"ewald.thamer#t-online.de","address":{"#type":"PostalAddress","postalCode":"65931","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Hesselbergweg 30"},"telephone":"(069) 36402623"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"Falke Designs","email":"info#falke-designs.de","address":{"#type":"PostalAddress","postalCode":"60486","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Kaufunger Str. 16"},"telephone":"(0173) 8605872"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"FarbHey GmbH & Co.KG Malerfachbetrieb","url":"https:\/\/www.farbhey.de","email":"kontakt#farbhey.de","address":{"#type":"PostalAddress","postalCode":"60433","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Am Gr\u00fcnen Graben 20"},"telephone":"(069) 516309"}},{"#type":"ListItem","item":{"#type":"LocalBusiness","name":"FBM Boras Facility & Bau Management GmbH","url":"https:\/\/www.fbmgmbh.de","email":"info#fbmgmbh.de","address":{"#type":"PostalAddress","postalCode":"60594","addressLocality":"Frankfurt","addressRegion":"Hessen","streetAddress":"Darmst\u00e4dter Landstr. 4"},"telephone":"(069) 63159155"}}],"name":"Alle Ergebnisse f\u00fcr Handwerker in Frankfurt online vergleichen"}}
console.log( d.mainEntity.itemListElement[0].item.email)
I am trying to build google dynamic remarketing for online store and I have problem with many product id's in cart.
Here is my code:
{literal}
<script type="text/javascript">
var ids = "{/literal}{foreach from=$products|json_decode key=pId item=p name=i}{$p.id_product}{if not $smarty.foreach.i.last}, {/if}{/foreach}{literal}";
var total = "{/literal}{displayPrice price=$total_products_wt}{literal}";
total = total.replace(/[zł]/g, "");
total = total.replace(/[,]/g, ".");
dataLayer.push({
'event': 'fireRemarketingTag',
'google_tag_params': {
'ecomm_prodid': ids,
'ecomm_pagetype': 'cart',
'ecomm_totalvalue': total
}
});
</script>
{/literal}
Still getting error from google tag assistant - "Multiple product IDs need to be stored in an array."
it looks like this
ecomm_prodid:367\, 74, ecomm_pagetype:cart, ecomm_totalvalue:109.80
"ecomm_prodid": "367,
74",
it should look like this;
ecomm_prodid:367,74, ecomm_pagetype:cart, ecomm_totalvalue:109.80
"ecomm_prodid": [
367,
74
],
I think the only thing you need to do, is to change the " characters around the var ids definition to [ and ], like this:
var ids = [{/literal}{foreach from=$products|json_decode key=pId item=p name=i}{$p.id_product}{if not $smarty.foreach.i.last}, {/if}{/foreach}{literal}];
I did a lot of searching and tried eleventy-billion different Google search combinations, but all I can find on this issue is how to set a default option in a select box.
I have a page where an admin can select a user from a list of users, and then Angular JS grabs the user_id of that user (using ng-change), sends it to the DB via POST, and then the goal is to change the value of the other inputs to the values from the DB. I have the values, but running into a hitch when using that value to get my state select box to change to the user's state.
The JS in question:
$scope.getUserInfo = function(user_id) {
this.user_id = user_id;
$http.post("lib/scripts/managing_user.php", {func: 'retrieve_user_info', user_id: this.user_id}).
success(function(data) {
console.log(data);
$scope.is_active = data['0']['active'];
//Interacts with the ng-checked directive. It takes a bool, and data returns 0 or 1 in active.
$scope.username = data['0']['username'];
//Assigns the data value to the ng-model directive with the value username
//Have to treat data as a 2D array because that is how it is returned
$scope.email = data['0']['email'];
$scope.fName = data['0']['first_name'];
$scope.lName = data['0']['last_name'];
$scope.schoolList = data['0']['school_id']; (<-Does not work)
}).
I accomplished the same thing using jQuery before. Here is the code if it helps you see what I want to do.
if ($("#school").children("option:selected"))
$("#school").children("option:selected").attr("selected", "false");
$("#school #" + this['school_id'] + "").attr("selected", "true");
Here is the Select Box that I want changed.
<div class="row-fluid">
<span class="adduser_heading">School:</span>
<select id="school" class="adduser_input" ng-model="user.schoolList" ng-options="name.school_name for (key, name) in schoolList" ng-disabled="is_disabled" name="school" style="width: 246px;"></select>
</div>
I get the list of schools from the DB, and that populates that list. After selecting a user, I want this select box to change to that user's school. The ultimate goal is for the admin to be able to change the selected school and submit it, changing the school in the DB.
Hope I described the problem adequately. Basically: I want to select an option in a select box from the JS using Angular JS.
Edit: As per the advice of oware, I created a function that gets just the school name from the object array and returns it to $scope.user.schoolList. Unfortunately, that did not work.
$scope.user.schoolList = $scope.findInSchoolList(data['0']['school_id']);
$scope.findInSchoolList = function(school_id) {
var school_id = school_id;
var school;
school = $scope.schoolList[school_id];
school = school['school_name'];
return school;
};
And here is the format of what is returned from the DB with regards to school. I don't really want to post "data" since that has the information of an actual person. Basically, the information with regards to school is what is below.
school_id: "106"
school_name: "Central Campus High School"
Your ng-model is set to user.schoolList, while you're assigning the default value to $scope.schoolList.
It should be $scope.user.schoolList instead.
If you want to use the find function, you still need to return the right object, not just the name; and you need to fix your function. So something like this should work:
$scope.findInSchoolList = function(school_id) {
for(var i = 0; i < $scope.schoolList.length; i++) {
if ($scope.schoolList[i].school_id == school_id) {
return $scope.schoolList[i];
}
}
};
Here's a working example:
angular.module('app', [])
.controller('Ctrl', function($scope) {
var findInSchoolList = function(school_id) {
for (var i = 0; i < $scope.schoolList.length; i++) {
if ($scope.schoolList[i].school_id == school_id) {
return $scope.schoolList[i];
}
}
};
$scope.schoolList = [{
school_id: "1",
school_name: "Central Campus High School"
}, {
school_id: "106",
school_name: "Another High School"
}, {
school_id: "231",
school_name: "Yet Another High School"
}, {
school_id: "23",
school_name: "The Correct High School"
}, {
school_id: "2",
school_name: "Last High School"
}]
$scope.user = {
schoolList: findInSchoolList(23)
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div class="row-fluid" ng-app="app" ng-controller="Ctrl">
<span class="adduser_heading">School:</span>
<select id="school" class="adduser_input" ng-model="user.schoolList" ng-options="name.school_name for (key, name) in schoolList" ng-disabled="is_disabled" name="school" style="width: 246px;"></select>
</div>
you have to select the item from the array that populated the list, for example, if you have:
$scope.school_list = [{id:1, name:'harvard'}, {id:2, name:'other'}]
and you want to select with:
$scope.user.schoolList = {id:1, name:'harvard'}
it won't work, you have to make a fucntion that finds the element in the array and then assign it to the $scope.user.schoolList variable (that is bound to the ng-model of your list)
you have to do something like this:
$scope.user.schoolList = findInSchoolList({id:1, name:'harvard'})
and it will select the item from the select list
hope it helps
I want to parse the JSON response of Country & State from below mentioned URL.
https://gist.githubusercontent.com/mayurah/5f4a6b18b1aa8c26910f/raw/countriesToCities.json
{
"China": [
"Guangzhou",
"Fuzhou",
"Beijing",
"Baotou",
"Hohhot",
"Guiyang",
"Yinchuan",
"Nanjing",
"Changzhou",
"Chuzhou",
"Hefei",
"Jinan",
"Qingdao",
"Harbin",
"Zhaodong",
"Taiyuan",
"Xi'an",
.
.
.
}
]
I want the result as,
Guangzhou, China
Taiyuan, China
I am using AngularJS, Following is a piece of code of my controllers.js file.
var
apiUrl = {
countryStateList: 'https://gist.githubusercontent.com/mayurah/5f4a6b18b1aa8c26910f/raw/countriesToCities.json'
};
countryData.controller('countryDataCtrl', ['$scope', '$http', function($scope, $http) {
$http.get(apiUrl.countryStateList).success(function(data) {
// console.log(data);
$scope.countryStateList = data;
});
}]);
And, in my HTML file, code is something like this,
<ul ng-repeat="country in countryStateList">
<li>{{ STATE_NAME, COUNTRY_NAME }}</li>
</ul>
STATE_NAME & COUNTRY_NAME are just for in display information. Both must be replaced with Angular code.
The problem is JSON element names are itself country names. Can someone tell me how to parse the same in AngularJS/Javascript.
You could start by parsing the whole JSON:
var jsonObject = JSON.parse(RAW_JSON_STING);
var newJsonObject = [];
// Loop through it
for (var interator in jsonObject)
{
for (var it = 0; it < iterator.length; it++)
{
newJsonObject.push(iterator[it] + ", " + iterator);
}
}
No guarantee the code above will work as intended.
Ng-repeat will not be useful in your case. This would be possible using directive.
<datalist id="stateCountry">
<custom-datalist-option data-array="countryStateList"></custom-datalist-option>
</datalist>
Link a directive function and create an option using using it.
Here is the Update Fiddle
Hope this will help you.