I am creating a Line Chart with d3 in Angular 8.
I following some example with TEMPERATURES. But my API is getting following format.
Data from API
{
"status": 200,
"data": {
"dashboard_data": [
[
2014-04-01,
2920.0,
239.44000000000003
],
[
2014-04-02,
2260.0,
185.32000000000002
],
[
2014-04-03,
156.0,
12.792
],
[
2014-04-04,
980.0,
80.36
],
[
2014-04-05,
1515.0,
124.22999999999999
],
],
}
}
Need data in this format
TEMPERATURES = [
{
'values': [
{'date': new Date('2012-09-05'), 'temperature': 77.7},
{'date': new Date('2012-09-06'), 'temperature': 74.2},
{'date': new Date('2012-09-07'), 'temperature': 76.0},
]
},
];
I am looking for solution, how I convert dashboard_data with values?
TIA
Try like this:
TEMPERATURES = [];
constructor() {
let obj = {};
obj["values"] = this.input.data.dashboard_data.map(item => ({
date: new Date(item[0]),
temperature : item[2]
}))
this.TEMPERATURES.push(obj)
}
Working Demo
It is not clear how to convert temperature. However, you can try the following approach:
let temperatures = [];
const result = obj.data.dashboard_data
.map(([d, h, t]) => ({date: new Date(d), temperature: t}));
temperatures.push({values: result});
An example:
let obj = {
"status": 200,
"data":
{
"dashboard_data": [
[
2014 - 04 - 01,
2920.0,
239.44000000000003
],
[
2014 - 04 - 02,
2260.0,
185.32000000000002
],
[
2014 - 04 - 03,
156.0,
12.792
],
[
2014 - 04 - 04,
980.0,
80.36
],
[
2014 - 04 - 05,
1515.0,
124.22999999999999
],
]
}
};
let temperatures = [];
const result = obj.data.dashboard_data
.map(([d, h, t]) => ({date: new Date(d), temperature: t}));
temperatures.push({values: result});
console.log(temperatures);
You have to iterate over each item in dashboard_data array and then pick date and temperature from it and push it to TEMPERATURE array:
dashboard_data.forEach((item)=>{
TEMPERATURES.push({'date': new Date(item[0]), 'temperature': item[2]});
}
)
let dashboard_data = [
[
'2014-04-01',
2920.0,
239.44000000000003
],
[
'2014-04-02',
2260.0,
185.32000000000002
],
[
'2014-04-03',
156.0,
12.792
],
[
'2014-04-04',
980.0,
80.36
],
[
'2014-04-05',
1515.0,
124.22999999999999
],
]
let TEMPERATURES = [];
dashboard_data.forEach((item)=>{
TEMPERATURES.push({'date': new Date(item[0]), 'temperature': item[2]});
}
)
console.log(TEMPERATURES);
Related
I need to convert the following array data to Json in react.
I tried map method, but it is not the correct way. I need key value pair, so that i can pass it to server as json
[
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
]
Try using reduce and creating an object:
var arr = []; // your array
var data = arr.reduce((map, item) => {
map[item[0]] = item[1];
return map;
}, {});
The data object will be in the following format:
{
"accountAdministration": "Partnership 1",
"activationDate": "2020-10-29T05:54:00.000Z",
"category": "Account 1",
"channel": "s1",
...
}
Worked with
Object.fromEntries([
[
"channel",
"s1"
],
[
"category",
"Account 1"
],
[
"accountAdministration",
"Partnership 1"
],
[
"partnershipAccounting",
"1 level Performance issues"
],
[
"requestCategory",
"Research"
],
[
"severity",
"Blocker"
],
[
"activationDate",
"2020-10-29T05:54:00.000Z"
],
[
"managerApproved",
true
]
])
I am trying to query documents from a mongodb collection, based on array of input query parameters sent from URL.
Sample Database Data
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [
{
"id": "827",
"name": "circle"
}
],
"square": [],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Input Query Parameter:
query = ["square","cube"];
Expected Output:
[
{
"drawings": {
"circle": [],
"square": [
{
"id": "828",
"name": "square"
}
],
"cube": []
},
{
"drawings": {
"circle": [],
"square": [],
"cube": [
{
"id": "829",
"name": "cube"
}
]
}
]
Best suited Mongoose Query:
Schema.find({
$or:[
{'drawings.square':{$elemMatch:{ name:'square'}}},
{'drawings.cube':{$elemMatch:{ name:'cube'}}}
]
});
Tried Below method. But, it is not correct.
let draw = ["square","cube"];
let draw_query =[];
for (let a=0; a<draw.length;a++){
draw_query.push("{\"drawings."+ draw[a] +"\':{$elemMatch:{ name:\"" + draw[a] + "\"}}}");
}
It creates array with single quoted strings. It cannot be used.
[ '{"drawings.square":{$elemMatch:{ name:"square"}}}',
'{"drawings.cube":{$elemMatch:{ name:"cube"}}}' ]
How to generate this mongoose query dynamically? or is there any better mongoose query to achieve the expected result.
You can query it directly using dot notation so the query should look like below:
db.collection.find({
$or: [
{
"drawings.square.name": "square"
},
{
"drawings.circle.name": "circle"
}
]
})
You can build it in JS using .map(), try:
var query = ["square","cube"];
var orQuery = { $or: query.map(x => ({ [x + ".name"]: x }) ) }
I'm trying to parse JSON which is like below:
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
Please help me deal with this. And is there a possibility to decode the timestamp value to a date.
Correct json
{
"price": [
[
1539283140000,
6288.07
],
[
1539283440000,
6285.82
],
[
1539283740000,
6285.81
],
[
1539284041000,
6280.37
],
[
1539284340000,
6280.19
]
]
}
struct Root: Codable {
let price: [[Double]]
}
let res = try? JSONDecodable().decode(Root.self,from:jsonData)
I'm having trouble getting to object in json file.
I'm using
.controller("ListController", ["$scope", "$http", function($scope, $http){
$http.get('../scripts/bbtv.json').success(function(data) {
$scope.artists = data;
});
}])
The data gets in the variable, but I can't access any object of it. Here is part of the json file. For example how to print out {{programmeField.titleField.valueField}} ?
{
"channelField": [
{
"displaynameField": [
{
"valueField": "bTV"
}
],
"idField": "BBTV"
}
],
"programmeField": [
{
"titleField": [
{
"langField": "BULG",
"valueField": "Тази сутрин"
}
],
"subtitleField": [],
"creditsField": {
"moderatorField": [
"Антон Хекимян"
]
},
"categoryField": [
{
"langField": "BULG",
"valueField": "Информационно предаване"
},
{
"langField": "BULG",
"valueField": "Сутрешен блок"
}
],
"languageField": {
"valueField": "BULG"
},
"lengthField": {
"unitsField": 1,
"valueField": "180"
},
"videoField": {
"presentField": "yes",
"colourField": "yes",
"aspectField": "4:3",
"qualityField": "800x600"
},
"audioField": {
"presentField": "yes",
"stereoField": "no",
"dolbyDigitalField": "no",
"dolbySurroundField": "no"
},
"startField": "20151216063000 +0200",
"stopField": "20151216093000 +0200",
"channelField": "BBTV",
"clumpidxField": "0/1",
"_photos": [
{
"_id": "5f38a2ab2fedd6b0e48da60b833bb4ddb69d3a1c",
"_url": "***.jpg",
"_type": "Letterbox"
}
],
"_deleted": false,
"_id": "189397717",
"_contentId": 45207610,
"_broadcastdate": "20151216"
}
}
JSON is a transport format. Once it's decoded, it's a native data structure, like any other structure. Since you're in JS, use JS conventions, and follow the bracketing/bracing. Note the labelling on the objects/arrays below:
data = { "channelField": [
a b
{ "displaynameField": [
c d
{ "valueField": "bTV"
e
}
],
"idField": "BBTV"
data.channelField[0].displaynameField[0].valueField -> "bTV"
a b c d e
Since your "programmeField" and "titleField" are array, use programmeField[0].titleField[0].valueField
In future you can copy your json to http://jsonviewer.stack.hu/ and the click on the Viewer tab.
Here is the my JSON Array format
[
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
The "1234","4567" and "7890" are my Ids and "Name11","Name12", "Name13", "Name21", "Name22" and "Name23" are the values for each corresponding Ids.
The JSON array contains two records as shown above. Now i need to convert the following JSON Format....
[
{
"1234":"Name11",
"4567":"Name12",
"7890":"Name13"
},
{
"1234":"Name21",
"4567":"Name22",
"7890":"Name23"
}
]
Please help me out, how can i construct the above mentioned JSON array format.
With pure javascript:
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
]
var newObj = []
for(var i = 0; i< src.length; i++){
var item = {}
for(var j = 0; j<src[i].length; j++){
if(j%2 === 0){
item[src[i][j]] = ""
}else{
item[src[i][j-1]] = src[i][j][0]
}
}
newObj.push(item)
}
You can do it with the following code (see the snippet below):
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
var src = [
[
"1234",
[
"Name11"
],
"4567",
[
"Name12"
],
"7890",
[
"Name13"
]
],
[
"1234",
[
"Name21"
],
"4567",
[
"Name22"
],
"7890",
[
"Name23"
]
]
];
var res = src.map(function(a){
var arr = [];
for(var i=0;i<a.length/2;i++) {
var item = {};
item[a[i*2]] = a[i*2+1][0];
arr.push(item);
};
return arr;
});
document.getElementById("res").textContent = JSON.stringify(res);
<div id="res"></div>