How to acces array object in nodejs? - arrays

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;

Related

AngularJS: Read array with variables with dot

I have the following result from database (using sequelize), the result:
JSON from the database:
[{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]
In the controller I do the next:
vm.spareParts = data.data;
console.log('-------- ' + JSON.stringify(vm.spareParts));
for (var i = 0; i < vm.spareParts.length; i++) {
vm.items.push({
codSparePart: vm.spareParts[i].codSparePart,
name: vm.spareParts[i].name,
description: vm.spareParts[i].description,
available: vm.spareParts[i].available,
codManufacturer: vm.spareParts[i].codManufacturer,
nameManufacturer: vm.sparePart[i]['manufacturer.description'],
stock: vm.spareParts[i].stock,
codProject: vm.spareParts[i].codProject
});
All is working perfectly except in reading the variables with dot.
nameManufacturer: vm.sparePart[i]['manufacturer.description']
nameManufacturer: vm.sparePart[i]['manufacturer.name']
How can I read these variables, I'm looking for the solution but I think is too simple and there is not any information about it.
Thanks in advance for your help.
You have a typo, change vm.sparePart to vm.spareParts
This line: nameManufacturer: vm.sparePart[i]['manufacturer.description'],
Your code seems to be working perfectly:
const array =[{"codSparePart":"SP001","name":"NAME","description":"DESCRIPTION","codProject":1,"available":1,"codManufacturer":1,"stock":4,"manufacturer.name":"MAN1","manufacturer.description":"DES1","manufacturer.codManufacturer":1}]
console.log(array[0]['manufacturer.description']);
More information: How to get JSON objects value if its name contains dots?

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

Parse url string object into host name in 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

Implementing piwik.js custom event sending to service

Is it possible to use Piwik for tracking but to process and send events to custom service instead of Piwik one?
Also make POST instead of GET requests to service?
Yes, something like that is possible. Piwik has setCustomRequestProcessing function. That function lets you edit the tracking request before it is sent.
As far as POST, you can get query parameters from piwiks tracking request. Something like this:
var getQueryParameters = function (hash) {
var split = hash.split('&');
var obj = {};
for (var i = 0; i < split.length; i++) {
var kv = split[i].split('=');
obj[kv[0]] = decodeURIComponent(kv[1] ? kv[1].replace(/\+/g, ' ') : kv[1]);
}
return obj;
};

Retrieve Coordinates from Array

I would like to put all my coordinates into an array and have a loop display each one but with a Google function.
Here's the code, which draws the points on google map:
var flightPlanCoordinates = [
new google.maps.LatLng(37.772323, -122.214897),
new google.maps.LatLng(21.291982, -157.821856),
new google.maps.LatLng(-18.142599, 178.431),
new google.maps.LatLng(-27.46758, 153.027892)
];
I would like to be able to do something like this:
var arrPos = new Array([37.772323, -122.214897], [21.291982, -157.821856], [-18.142599, 178.431], etc. );
var flightPlanCoordinates = [ +
for (i=0; i<arrPos.length; i++){
new google.maps.LatLng(arrPos[0]) + ", "
}
+ "];"
I know you can put a loop in an array but is there an alternate method to retrieving the points from the array??
tks
Like this:
For(var i in arrPos){
new google.maps.LatLng(arrPos[i][0],arrPos[i][1]);
}
Greets
Thomas van Latum

Resources