I have a weather plugin that outputs in a weird way [closed] - javascript

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
Like this: https://ghostbin.co/paste/ohsuar
I only need the current temperature value. How do I get it?
Let's say that "weatherObject" is the variable with the object. I have already tried "weatherObject.current.temperature" and it outputs "undefined".
[
{
"location": {
"name": "New York, NY",
"lat": "40.713",
"long": "-74.007",
"timezone": "-4",
"alert": "",
"degreetype": "C",
"imagerelativeurl": "http://blob.weather.microsoft.com/static/weather4/en-us/"
},
"current": {
"temperature": "14",
"skycode": "27",
"skytext": "Cloudy",
"date": "2021-05-02",
"observationtime": "03:00:00",
"observationpoint": "New York, NY",
"feelslike": "15",
"humidity": "40",
"winddisplay": "16 km/h Southwest",
"day": "Sunday",
"shortday": "Sun",
"windspeed": "16 km/h",
"imageUrl": "http://blob.weather.microsoft.com/static/weather4/en-us/law/27.gif"
},
...
}
]

There are many Objects in your weatherObject-array. So you have to call it with the index of the array-element you want to get. Furthermore the keys of the nested objects are strings so you can't call it with the dot-notation. You have to use square brackets.
For the first element of the array it would be:
weatherObject[0]["current"]["temperature"]
Working example:
var weatherObject = [
{
"current": {
"temperature": "14"
}
}
];
console.log(weatherObject[0]["current"]["temperature"]);

Related

How to convert array to single object? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 months ago.
Improve this question
I have array object like this.
[
{
"name": "name1",
"type": "type1",
"car": "car1",
"speed": 1
},
{
"name": "name1",
"type": "type1",
"car": "car2",
"speed": 2
},
]
And i want to convert it single object for prevent data replay.
How can i do this?
{
"name": "name1"
"type": "type1",
"car": [
"car1",
"car2",
],
"speed": [
1,
2,
],
}
Could someone help me? Thanks.
If name and type can have same value and car and speed various,then below is a reference for you
Note: in order to get an expected answer(solution),you need to make you question more clear,the most important thing is the value of which properties various
const data = [
{
"name": "name1",
"type": "type1",
"car": "car1",
"speed": 1
},
{
"name": "name1",
"type": "type1",
"car": "car2",
"speed": 2
},
]
const result = data.reduce((a,v) =>{
let obj = a.find(i => i.name == v.name)
if(obj){
obj.car.push(v.car)
obj.speed.push(v.speed)
}else{
a.push({'name':v.name,'type':v.type,'car':[v.car],'speed':[v.speed]})
}
return a
},[])
console.log(result)

Logic to covert json stringify into multiple array [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
var result = [
{
"Name": "abc",
"age": 12,
"City": "Mumbai",
"id":"U121"
},
{
"Name": "jkl",
"age": 22,
"City": "Mumbai",
"id":"U122"
},
{
"Name": "xyz",
"age": 32,
"City": "Mumbai",
"id":"U123"
},{
"Name": "mno",
"age": 42,
"City": "Mumbai",
"id":"U124"
}
]
I want to iterate this in javascript and create td, tr element in such a way that abc, jkl come in one row and xyz, mno come in second row. Can anyone help me with the logic?
You can try something like this
var input = [
{
"Name": "abc",
"age": 12,
"City": "Mumbai",
"id":"U121"
},
{
"Name": "jkl",
"age": 22,
"City": "Mumbai",
"id":"U122"
},
{
"Name": "xyz",
"age": 32,
"City": "Mumbai",
"id":"U123"
},{
"Name": "mno",
"age": 42,
"City": "Mumbai",
"id":"U124"
}
];
function test(){
let i=0;
let outhtml="";
input.forEach(ele => {
i=i%2;
let addRow=(i==0);
//After 2 elements add row
if (addRow){
outhtml += "</tr><tr>"
}
//add td
outhtml += "<td>"+ele.Name+"</td>"
i++;
})
//Remove </tr> from start
outhtml = outhtml.substring(5, outhtml.lenght);
document.getElementById("mytable").innerHTML = outhtml;
}
test();
<table id="mytable">
</table>

How to write only pure JS(no jquery or anything) code for reading JSON data from external '.js' file? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
Note : Using only pure JS, no addons, please provide a sample example
Sample data js file content -
var TABLE_DATA = [
{
"id": "5",
"name": "cony #5",
"thumbnailUrl": "image/5.gif",
"price": 170
},
{
"id": "1",
"name": "cony #1",
"thumbnailUrl": "image/1.gif",
"price": 170
},
{
"id": "2",
"name": "cony #2",
"thumbnailUrl": "image/2.gif",
"price": 270
},
{
"id": "8",
"name": "cony #8",
"thumbnailUrl": "image/8.gif",
"price": 70
},
{
"id": "10",
"name": "<img onerror='window.document.body.innerHTML = \"<h1>XSS</h1>\";' src=''> ",
"thumbnailUrl": "image/10.gif",
"price": 170
},
{
"id": "4",
"name": "cony #4",
"thumbnailUrl": "image/4.gif",
"price": 150
},
{
"id": "3",
"name": "cony #3",
"thumbnailUrl": "image/3.gif",
"price": 130
},
{
"id": "6",
"name": "cony #6",
"thumbnailUrl": "image/6.gif",
"price": 160
},
{
"id": "9",
"name": "cony #9",
"thumbnailUrl": "image/9.gif",
"price": 170
},
{
"id": "7",
"name": "cony #7",
"thumbnailUrl": "image/7.gif",
"price": 170
}
]
js has inbuilt JSON.parse(YOUR_JSON_FILE) method for parsing json.
In your example it is a json array.
So you may use for loop to go through each individual jsons.
const length = TABLE_DATA.length;
for(var i =0 ;i< length;i++) {
var jsObject = JSON.stringify(TABLE_DATA[i]);
console.log(jsObject);
//To read id
console.log(jsObject.id)
//To read name
console.log(jsObject.name)
//and so on
}
REMEMBER : JSON.parse turns a string of JSON text into a JavaScript
object
JSON.stringify turns a JavaScript object into JSON text and stores
that JSON text in a string
To LOAD_IN data from file click here : Loading Local Json File
May it Help You!
To load your external file, it must be made available on some place that your script is able to access (e.g. a web server). If you place the data as pure JSON (without var TABLE_DATA), you can load this data using the XMLHttpRequest, something along the lines of the following:
const xhttp = new XMLHttpRequest()
xhttp.onreadystatechange = function() {
if (this.readyState ==4 && this.status==200) {
data = JSON.parse(this.responseText)
// do stuff with your data
}
}
xhttp.open(URL_TO_THE_JSON)
xhttp.send()

hierarchical json data from flat json [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I have parent-child JSON data and I want to get all children (nested children) from a selected parent in the specific year-wise data format as below
var data = [{ "Id": "1", "Name": "abc", "EnteredOn": "03/01/2019", "attr": "abc" },
{ "Id": "2", "Name": "abcd", "EnteredOn": "02/01/2018", "attr": "abc" },
{ "Id": "3", "Name": "abcde", "EnteredOn": "04/01/2019", "attr": "abc" },
{ "Id": "4", "Name": "abcdef", "EnteredOn": "01/01/2016", "attr": "abc" }];
output:
{
"year": "2019",
"children": [
{
"ID": "1",
"Name": "abc",
"Date": "03/01/2019"
}
]
}
First, i recommend adding year attribute
data.forEach(a=> a['year'] = a.EnteredOn.getFullYear()
and using Lodash _.groupBy function
data.groupBy("year")

JavaScript: How can I transform objects array A to objects array B in the efficiency way? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I got object array A as this:
[
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Voltage",
"Value": "230.12"
},
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Frequency",
"Value": "50.12"
},
{
"Timestamp": "2015-10-01 00:00:00",
"Label": "Power",
"Value": "23"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Voltage",
"Value": "231.12"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Frequency",
"Value": "51.12"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Label": "Power",
"Value": "23.4"
}
]
I want to transform to object array B as this:
[
{
"Timestamp": "2015-10-01 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
},
{
"Timestamp": "2015-10-02 22:22:22",
"Voltage": "231.12",
"Frequency": "51.12",
"Power": "23.4"
}
]
I was looping to get the timestamp and re-loop again to get label and value to form new object array. It works but when the data become few hundred thousands object array then it's not efficiency and crash the browser. Would someone can think of a better way please. Thanks very much.
JSON is just a transport/storage format. It's generally not something you should attempt to manipulate directly. Best to manipulate the actual JavaScript objects/arrays.
If you can guarantee the original array sort order, try something like this:
var outputArray = [];
var currentObj = null;
originalArray.forEach(function (measurement) {
if (!currentObj || currentTimestamp !== measurement.Timestamp) {
currentTimestamp = measurement.Timestamp;
currentObj = {
Timestamp: measurement.Timestamp;
}
outputArray.push(currentObj);
}
currentObj[measurement.Label] = measurement.Value;
});
Then, you can just loop through and build the new array as you go.
As your desired output showed ignoring the time of the timestamp this will trim that part of the timestamp - if that was a mistake in the question I think you should be able to remove that part by yourself
var b = a.reduce((acc, cur) => {
var strippedTimestamp = cur.Timestamp.substring(1, 10) + " 00:00:00";
var obj = acc.find(e => e.Timestamp == strippedTimestamp);
if (!obj) {
obj = { Timestamp: strippedTimestamp };
acc.push(obj);
}
obj[cur.Label] = cur.Value;
return acc;
}, []);
will output
[
{
"Timestamp": "015-10-01 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
},
{
"Timestamp": "015-10-02 00:00:00",
"Voltage": "230.12",
"Frequency": "50.12",
"Power": "23"
}
]

Categories

Resources