Parse url string object into host name in AngularJS - angularjs

I have a scope variable that returns me an absolute url stored by the user. While displaying I would like to only show the host name for the given object.
for example.
$scope.url ="www.myurl.com/zyas?nxs"
i want to only display it as www.myurl.com.
How can I achieve this?

Have a look at the below code
string s = "www.myurl.com/zyas?nxs";
string newstr = s.split("/"); //newstr == "www.myurl.com"

You can do like this:
$scope.url ="www.myurl.com/zyas?nxs"
$scope.host = $scope.url.split('/')[0];

var hostName = $scope.url.split('/')[0];

I went with the normal split provided by JavaScript. Here's how I parsed it to the hostname and pushed it back to my JSON object.
$scope.blogposts = result.data
// console.log(urls);
$scope.blogposts.forEach(function(blog){
console.log(blog);
var a = blog.url.split('/')
// blog.push({'viewUrl':'a[0]'})
blog["viewUrl"] =a[0]
})

In a better way, you can do this without splitting and doing things
var url = new URL("http://www.myurl.com/zyas?dsfadf");
console.log(url.host); // gives the host name www.myurl.com

Related

how to cast json to array in angular

I have file1.ts which has a variable menuGroups
menuGroups= Array();
i populate menuGroups with data so it looks something like this
i store this in localstorage : localStorage.setItem("menuGroups", JSON.stringify(this.menuGroups));
now in file2.ts, i want to assign above localstorage value to an array variable at class level. how to achieve it?
basically , in file2.html, i want to loop through the array menuGroups variable using ngFor.
i tried this in file2.ts but getting error
menuGroups: any[] = [];
populateNavBar() {
this.menuGroups = JSON.parse(localStorage.getItem('menuGroups')) || [];
}
Working ex
If you try to change
menuGroups: any;
Show run example stackblitz here.

How to acces array object in nodejs?

I am getting this req in my req.body . I want to parse the details .Below is my req.
"[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
I want output like
number:
cmdb_ci:
category:
How do i parse this array object in nodejs. Please help
Use JSON.parse() like this:
var aJsonArrString = "[{\"number\":\"INC0010075\",\"cmdb_ci\":\"hubot-test\",\"short_description\":\"test data for buisness rule 30\",\"category\":\"software\",\"comments\":\"\"}]"
var aObjList = JSON.parse(aJsonArrString);
for(var i = 0; i < aObjList.length; i++) {
console.log('number : ' + aObjList[i].number);
console.log('cmdb_ci : ' + aObjList[i].cmdb_ci);
console.log('category : ' + aObjList[i].category);
}
You Can Use
JSON.parse(req.body);
This looks like JSON, I don't know if the escaping \ are coming from your way of logging the value or something so I'll expect it is a valid string to start off.
You can use
var my_list = JSON.parse(req.body);
//Access like any other array...
my_list[0].number;

How to convert dictionary to a string angularjs

I am currently working on my project using angularjs. I got everything already it is just that, i need to convert the dictionary list to a string separated by comma. I can only do this using python.
[{"name":"john"},{"name":"mark"},{"name":"peter"}]
I want to convert them to string
"john,mark,peter"
I would really appreciate your help. :)
.map and then .join will do
var array = [{"name":"john"},{"name":"mark"},{"name":"peter"}];
var names = array.map(function(item) {
return item.name;
}).join(',');
The map() method creates a new array with the results of calling a function for every array element. Use this to loop and then add that value to a variable.
var dict=[{"name":"john"},{"name":"mark"},{"name":"peter"}];
var string;
dict.map(function(value){
//do any stuff here
string+=value["name"]+",";
});
console.log(string);
Try map function to concatenate the values:
var dict=[{"name":"john"},{"name":"mark"},{"name":"peter"}];
var str="";
dict.map(function(a){
str+=a["name"]+",";
});
//feels ironical as question has AngularJS tag
document.getElementById("log").innerText=str;
<div id="log"></div>
You can simply iterate over each key-value pair and concat the extracted value with comma.
var obj = [{"name":"john"},{"name":"mark"},{"name":"peter"}]
var result = '';
for (var p in obj) {
if (obj.hasOwnProperty(p)) {
result += obj[p].name + ",";
}
}
result = result.replace(/,$/g,''); // to trim trailing comma

Convert String into Variable in AngularJS like in PHP variable variables

This is the outcome I am looking for:
$scope.donotShow = false;
The way I want to get it is in the controller in which I have one variable to work with:
var slug = "donot";
I would like to combine it with the string "Show":
var slugshow = slug + "Show";
From here I want the outcome I am looking for but this does not work:
$scope.slugshow = false;
How do I make the var slugshow show as "donotShow" thus giving me
$scope.donotShow = false;

Bug when sending array in node.js and socket.io

I use socket.io version 0.8.4
I have boiled down my problem to the following. I have data looking like this:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = [];
data.prop2["hey"] = "man";
I send the data from the server to the client this way:
socket.emit("data", data);
On the client side I receive the data this way:
socket.on("data", function(data){ console.log(data); });
The weird thing is:
data.prop1 = [];
data.prop1.push("man"); // This data exists in the client side data object
data.prop2 = [];
data.prop2["hey"] = "man"; // This data does not exist.
data.prop2 is just an empty array on the client side.
Is there a known bug in json serializing arrays on the form in prop2?
Thankyou in advance
EDIT:
Problem solved using this workaround:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // <= Object instead of array
data.prop2["hey"] = "man";
ECMA-262 about JSON.stringify:
The representation of arrays includes only the elements between zero and array.length – 1 inclusive. Named properties are excluded from the stringification.
Arrays are supposed to have numerical property names. So when the data.prop2 is transformed to JSON (which socket.io sends the data in, I imagine), it doesn't get the 'hey' property. If you want to use non-numerical property names, you should use objects instead of arrays:
data.prop1 = [];
data.prop1.push("man");
data.prop2 = {}; // Notice we're creating an object, not an array.
data.prop2["hey"] = "man"; // Alternatively: data.prop2.hey = "man"
Unfortunately, Javascript doesn't really work like that.
Check out this article, about half way down. It explains the problem where you try to set data.prop2["hey"] = "man";

Resources