How get informations in javascript from API JSON? - javascript

I need help ! Can you tell me how can I get informations from this in javascript :
I would like to get the Third values from the fisrt array (121,73) and also the Fourth (99,25) . Thank you in advance !
{
"error": [],
"result": {
"XETHZEUR": [
[
1545955200,
"100.76",
"121.73",
"99.25",
"120.16",
"111.15",
"186385.05723331",
25420
],
[
1546041600,
"120.52",
"130.00",
"115.91",
"117.89",
"121.47",
"154551.36751227",
23261
],
"last": 1546387200
}
}

var feild = {
"error": [],
"result": {
"XETHZEUR": [
[
1545955200,
"100.76",
"121.73",
"99.25",
"120.16",
"111.14",
"183581.25666187",
25053
],
[
1546041600,
"120.16",
"130.00",
"115.91",
"117.89",
"121.48",
"152980.08236398",
21309
],
[
1546128000,
"117.96",
"123.75",
"112.78",
"121.99",
"119.43",
"108719.51659796",
14936
],
[
1546214400,
"121.99",
"122.30",
"113.00",
"114.96",
"118.14",
"69796.43117029",
9347
],
[
1546300800,
"114.97",
"123.90",
"114.00",
"122.46",
"119.83",
"64248.31302812",
9713
],
[
1546387200,
"122.62",
"138.80",
"121.25",
"135.57",
"131.69",
"189734.68299147",
37935
],
[
1546473600,
"135.57",
"136.25",
"128.33",
"131.45",
"132.12",
"67066.92874540",
8790
]
],
"last": 1546387200
}
}
let empty= [];
feild.result.XETHZEUR.map(e=>{
empty.push(e[2])
empty.push(e[3])
})
empty.map(e=>{
if(e=="121.73"){
console.log(e)
}
if(e=="99.25"){
console.log(e)
}
})

Related

How to filter an array using forEach() inside filter() - Javascript

I have an array of objects and I'd like to filter it based on another array. If filters object value is only one, I can use only filter, but if there are one more values in filters object, nothing show. So use forEach inside the filter and wanted to return fit values. Could you please help me what the issue is? Thanks
Expected result:
[
{
"tags": [
"madewithout__gluten",
"madewithout__nuts",
],
"metafields": "Side"
}
]
const data = [
{
"tags": [
"madewithout__gluten",
"madewithout__nuts",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__gluten",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__nuts"
],
"metafields": "Side"
}
]
const filters = ['gluten', 'nuts']
const result = data.filter((v) => {
filters.forEach((tag) => {
if(!v.tags.includes(`madewithout__${tag}`))
return;
});
});
console.log(result);
Based on the semantics of the data you provided, you want to check if all filters are contained in tags:
const result = data.filter((v) =>
filters.every((tag) => v.tags.includes(`madewithout__${tag}`))
)
const data = [
{
"tags": [
"madewithout__gluten",
"madewithout__nuts",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__gluten",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__nuts"
],
"metafields": "Side"
}
]
const filters = ['gluten', 'nuts']
const result = data.filter((v) =>
filters.every((tag) => v.tags.includes(`madewithout__${tag}`))
)
console.log(result);
Instead of .forEach, use .some
const result = data.filter((v) => {
return !filters.some((tag) => !v.tags.includes(`madewithout__${tag}`))
});
const data = [{
"tags": [
"madewithout__gluten",
"madewithout__nuts",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__gluten",
],
"metafields": "Side"
},
{
"tags": [
"madewithout__nuts"
],
"metafields": "Side"
}
]
const filters = ['gluten', 'nuts']
const result = data.filter((v) => {
return !filters.some((tag) => !v.tags.includes(`madewithout__${tag}`))
});
console.log(result);
Issues with your code
Array.forEach does not return anything. So your return does not break loop or return value.
Your Array.filter does not have return statement. Due to this, by default everything is falsy (return undefined) and hence empty array

Parsing JSON converted XML in Javascript

I am building an API that send a SOAP request to a web service in express, i used easy-soap-request to send SOAP request and naturally i got an XML response, then i used xml2js to convert the response. This was the response:
{
"s:Envelope": {
"$": {
"xmlns:s": "http://schemas.xmlsoap.org/soap/envelope/"
},
"s:Body": [
{
"SmartSearchIndividualResponse": [
{
"$": {
"xmlns": "some-url"
},
"SmartSearchIndividualResult": [
{
"$": {
"xmlns:a": "yet-another-url",
"xmlns:i": "http://www.w3.org/2001/XMLSchema-instance"
},
"a:IndividualRecords": [
{
"a:SearchIndividualRecord": [
{
"a:Address": [
"person-address"
],
"a:DateOfBirth": [
"person-DOB"
],
"a:FullName": [
"person-full-name"
],
"a:IDNumber": [
"person-ID-number"
],
"a:CompID": [
"company-ID"
]
}
]
}
]
}
]
}
]
}
]
}
}
is there anyway to extract some data like address or full name?
very bad JSON structure
const data =
{ 's:Envelope':
{ '$': { 'xmlns:s': 'http://schemas.xmlsoap.org/soap/envelope/'}
, 's:Body':
[ { 'SmartSearchIndividualResponse':
[ { '$': { 'xmlns': 'some-url'}
, 'SmartSearchIndividualResult':
[ { '$':
{ 'xmlns:a': 'yet-another-url'
, 'xmlns:i': 'http://www.w3.org/2001/XMLSchema-instance'
}
, 'a:IndividualRecords':
[ { 'a:SearchIndividualRecord':
[ { 'a:Address': [ 'person-address' ]
, 'a:DateOfBirth': [ 'person-DOB' ]
, 'a:FullName': [ 'person-full-name' ]
, 'a:IDNumber': [ 'person-ID-number' ]
, 'a:CompID': [ 'company-ID' ]
} ] } ] } ] } ] } ] } }
let individualRecs = data['s:Envelope']['s:Body'][0]
.SmartSearchIndividualResponse[0]
.SmartSearchIndividualResult[0]
['a:IndividualRecords'][0]
['a:SearchIndividualRecord'][0]
console.log( individualRecs['a:Address'][0] )
console.log( individualRecs['a:FullName'][0] )
.as-console-wrapper { max-height: 100% !important; top: 0; }

How to convert nested array of object into arraylist in javascript?

i have a nested array of object and i want to convert in arraylist like this :
this is my data array of object :
{
"status": true,
"message": "",
"data": [{
"pasien_docs": [{
"ecg": null,
"date": "2020-01-21T05:22:01.901Z"
}, {
"ecg": 1.03,
"date": "2020-01-21T05:22:02.979Z"
}, {
"ecg": 1.04,
"date": "2020-01-21T05:22:04.053Z"
}, {
"ecg": 1.04,
"date": "2020-01-21T05:22:05.126Z"
},
]
}
]
}
and i want change convert to array like this :
{
"status": true,
"message": "",
"data": [
[
"2020-01-21T05:22:01.901Z",
null
],
[
"2020-01-21T05:22:01.901Z",
1, 03
]
[
"2020-01-21T05:22:01.901Z",
1.04
]
[
"2020-01-21T05:22:01.901Z",
1.04
]
]
}
i try using map to convert on result like this :
result = result.map((u, i) => [
u.pasien_docs[i].date,
u.pasien_docs[i].ecg,
]);
but why i only get result data of one array not four data ? help me please, thankyou..
{
"status": true,
"message": "",
"data": [
[
"2020-01-21T05:22:01.901Z",
null
]
]
}
Would that work for you?
const src = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]},
result = {
...src,
data: src.data[0].pasien_docs.map(Object.values)
}
console.log(result)
.as-console-wrapper{min-height:100%;}
If you dont wanna use spread operator, this can also do the trick for you
const source = {"status":true,"message":"","data":[{"pasien_docs":[{"ecg":null,"date":"2020-01-21T05:22:01.901Z"},{"ecg":1.03,"date":"2020-01-21T05:22:02.979Z"},{"ecg":1.04,"date":"2020-01-21T05:22:04.053Z"},{"ecg":1.04,"date":"2020-01-21T05:22:05.126Z"},]}]}
const result = Object.assign({}, source, {
data: source.data[0].pasien_docs.map(Object.values)
})
console.log(result)
let obj = {
status: true,
message: "",
data: [
{
pasien_docs: [
{
ecg: null,
date: "2020-01-21T05:22:01.901Z",
},
{
ecg: 1.03,
date: "2020-01-21T05:22:02.979Z",
},
{
ecg: 1.04,
date: "2020-01-21T05:22:04.053Z",
},
{
ecg: 1.04,
date: "2020-01-21T05:22:05.126Z",
},
],
},
],
};
var finalobj = JSON.parse(JSON.stringify(obj));
var innerobj = obj.data;
var intermd = innerobj.map((data) => {
return data.pasien_docs;
});
finalarray = intermd[0].map((val) => {
return [val.ecg, val.date];
});
console.log(obj);
finalobj.data[0].pasien_docs=finalarray;
console.log(finalobj);

Remove object from array where sub-object matches a value

I'm currently attempting to sort through some values based on a query string. I'm able to target each target individually in this loop but cannot seem to properly remove them from the array. How can I remove certain values that match on target.target from targetList?'
targetList.forEach(function(target, key) {
var os = getParameterByName('os');
if (os == "android") {
if (target.target.includes(":IOS")) {
targetList.splice(key, 1);
}
} else if (os == "ios") {
if (target.target.includes(":ANDROID")) {
targetList.splice(key, 1);
}
}
if (target.target.includes(":IOS")) {
target.target = schools[target.target.replace(":IOS", "").toLowerCase()] + " (iOS)" || target.target;
}
if (target.target.includes(":ANDROID")) {
target.target = schools[target.target.replace(":ANDROID", "").toLowerCase()] + " (Android)" || target.target;
}
});
targetList contains an array like this:
[
{
"target": "t1",
"datapoints": [
[
51.0,
1483574400.0
],
[
54.0,
1485561600.0
],
[
54.0,
1485648000.0
]
]
},
{
"target": "t2",
"datapoints": [
[
56.0,
1484265600.0
],
[
70.0,
1484352000.0
],
[
71.0,
1484438400.0
],
[
51.0,
1484611200.0
]
]
},
]
What I'm attempting to do is that where a target matches certain criteria .includes() I want to remove the entire containing object/array.
It seems to me your code works just fine. I've created a snippet where I removed the extra processing, to show that what you have already does what you ask.
Is there a reason you're doing stuff to target.target once you've removed it from the array? Do you have a reference to the containing object somewhere else?
var targetList = [
{
"target": "t1",
"datapoints": [
[
51.0,
1483574400.0
],
[
54.0,
1485561600.0
],
[
54.0,
1485648000.0
]
]
},
{
"target": "t2",
"datapoints": [
[
56.0,
1484265600.0
],
[
70.0,
1484352000.0
],
[
71.0,
1484438400.0
],
[
51.0,
1484611200.0
]
]
},
];
targetList.forEach(function(target, key) {
if (target.target.includes("t2")) {
targetList.splice(key, 1);
}
});
console.log(targetList);

How do I draw a few lines with different styles on a leaflet map?

I have these lines called:
loadMapLayer(lines1);
loadMapLayer(lines2);
routesLayerGroup = L.layerGroup(routesMap).addTo(map);
and these functions:
function loadMapLayer(routesServer) {
for (var i = 0; i < routesServer.length; i++) {
if (routesServer[i].properties["type"] == "old") {
addRoute(routesServer[i], astar_style);
}
if (routesServer[i].properties["type"] == "new") {
addRoute(routesServer[i], bidi_style);
}
}
}
function addRoute(route, style) {
routeMap = L.geoJson(route, {
style: style
})
routesMap.push(routeMap);
}
var astar_style = {
"color": "red",
"weight": 5,
"opacity": 0.65
};
var bidi_style = {
"color": "yellow",
"weight": 5,
"opacity": 0.65
};
I debug and see addRoute(routesServer[i], astar_style); and addRoute(routesServer[i], bidi_style); are called, but on my map, all the routes are yellow. How can I fix this?
I have spread lines1 and lines2 to different start points and yet they are all yellow and not red.
See image:
Update:
I see the json I have doesn't fit to the leaflet object. route.properties["type"] returns undefined.
Why does parsing fail?
var lines2= [
{
"type": "LineString",
"stroke": "true",
"properties": {
"type": "new",
"alt_id": 0
},
"coordinates": [
[
-0.19260167,
51.60275078
],
[
-0.18776697865776057,
51.51378379618606
],
[
-0.1868361042991638,
51.51386350952752
],
[
-0.18610120486834886,
51.513949508589846
],
[
-0.1860126676721192,
51.5139587109798
],
[
-0.18503012435859684,
51.51407686278982
],
[
-0.1844715979628327,
51.512811327510235
],
[
-0.18438086668647774,
51.51260842537383
],
[
-0.18421945,
51.51219938
],
[
-0.18371567893492108,
51.51073654980558
],
[
-0.18367635639669866,
51.51051275309526
],
[
-0.18278586300375,
51.510576187561
],
[
-0.18001538515091456,
51.51099998178699
],
[
-0.17978213237246937,
51.511036465619824
],
[
-0.17754539181132,
51.511350689722
],
[
-0.17667213783413,
51.511469143954
],
[
-0.17605905,
51.5116603
],
[
-0.17599639,
51.51194254
],
[
-0.17595176,
51.5121633
],
[
-0.17576338,
51.51264908
],
[
-0.17526760982170084,
51.5127504306477
],
[
-0.17448749264235414,
51.512204418972296
],
[
-0.17366204281806927,
51.51184552510889
],
[
-0.17229427780151832,
51.51200799788328
],
[
-0.16879990859832764,
51.51236051966681
],
[
-0.1676648163893128,
51.51245737406541
],
[
-0.1680470167036,
51.513849950881
],
[
-0.16816666875569,
51.513895844874
],
[
-0.16806539030114,
51.513971712699
],
[
-0.16809616949711467,
51.514138665582394
],
[
-0.1670313325182289,
51.514318938428715
],
[
-0.16644342299303297,
51.51441707946363
],
[
-0.16557087028261,
51.515078135345
],
[
-0.1648144873404,
51.515515453665
],
[
-0.1645753,
51.51564283
],
[
-0.16412506782526948,
51.51588568011996
],
[
-0.16395137600442822,
51.51595497980128
],
[
-0.16371551862975017,
51.51601826243177
],
[
-0.16363422357553456,
51.51604007459718
],
[
-0.16248937,
51.51625975
],
[
-0.1618248006248185,
51.51636682729078
],
[
-0.16144195106065864,
51.516433011832596
],
[
-0.16079458,
51.51653877
],
[
-0.16049632,
51.51658803
],
[
-0.16013511,
51.51664799
],
[
-0.15952000080734252,
51.516760840000025
],
[
-0.15903941,
51.51683111
],
[
-0.15857268,
51.51690964
],
[
-0.15818745419376345,
51.51697458112003
],
[
-0.15752605,
51.51708608
],
[
-0.15710946680072266,
51.51715610050016
],
[
-0.15539842,
51.51744437
],
[
-0.1547544976616,
51.517548545154
],
[
-0.15355205591518,
51.517754494854
],
[
-0.15284529383976753,
51.51787800540975
]
]
},
Your code seems to do what you expect: http://jsfiddle.net/ve2huzxw/181/
Note that you could also simplify your code. The L.geoJson factory can directly manage your routesServer array of GeoJSON objects, and receive a style function which gets the feature as single argument.
function loadMapLayer(routesServer) {
routeMap = L.geoJson(routesServer, {
style: function(route) {
return route.properties["type"] == "old" ?
astar_style : bidi_style;
}
})
routesMap.push(routeMap);
}
Demo: http://jsfiddle.net/ve2huzxw/183/

Categories

Resources