As we know normally we can pass array as following to Flot Aria Chart.
var data2 = [
[0, 1], [1, 0], [2, 2], [3, 0], [4, 1], [5, 3], [6, 1], [7, 5], [8, 2], [9, 3], [10, 2], [11, 1], [12, 0], [13, 2], [14, 8], [15, 0], [16, 0]
];
I'm getting an json output from a file and i need to convert that as json object like 'data2' in upper code. i'm running a loop like this.
var data1 = [];
var json = $.getJSON( "config/tsconfig.json", function () {
$.each( json.responseJSON, function( i, item ) {
});
});
And inside of that json file like this.
[[2,"2017-09-27",50,30,25],[2,"2017-09-28",70,50,49],[3,"2017-09-27",50,45,5],[3,"2017-09-28",100,95,90]]
How can i make a json object as in code one. any suggestions to put inside $.each loop?
You have to use map method.
Also, $.getJSON is executing asynchronous, so you need to use a callback function.
getResponse(callback){
$.getJSON( "config/tsconfig.json", function (response) {
let result=response.map(function(item){
return [item[0],item[2]];
});
callback(result);
});
}
getResponse(function(response){
console.log(response);
});
let array=[[2,"2017-09-27",50,30,25],[2,"2017-09-28",70,50,49],[3,"2017-09-27",50,45,5],[3,"2017-09-28",100,95,90]]
array=array.map(function(item){
return [item[0],item[2]];
});
console.log(array);
Using arrow functions.
array=array.map(item=> [item[0],item[2]]);
Related
I have the following array:
const arr = [
[5, 0.2],
[7, 0.6],
[8, 0.3],
[10, 0.4]
];
console.log(arr)
I need to ensure that the first element of the array is a sequence from 5 to 10:
[5, 6, 7, 8, 9, 10]
In the above example, these numbers within the sequence are missing:
[6, 9]
If they are missing, I need to include them with zeros:
const expectedResult = [
[5, 0.2],
[6, 0],
[7, 0.6],
[8, 0.3],
[9, 0],
[10, 0.4]
];
console.log(expectedResult)
Any ideas on how to achieve this?
You could map the missing parts with a closure over the actual index of the given array.
const
array = [[5, 0.2], [7, 0.6], [8, 0.3], [10, 0.4]],
result = Array.from(
{ length: 6 },
(i => (_, j) => array[i]?.[0] === j + 5 ? array[i++] : [j + 5, 0])(0)
);
console.log(result);
I'm using Ascensor.js.
I build with this queued option:
var ascensor = $('#ascensor').ascensor({
direction: [[2, 2], [1, 1], [1, 2], [1, 3], [1, 4], [3, 1], [3, 2], [3, 3]],
queued: "y"
});
After arrive to a floor, I need to chage queued to "x", to go back the same way. This is possible?
thanks a lot
I found the solution.
ascensorInstance.options.queued = "x";`
I hope this help.
I just learned about JavaScript and here is my script:
var now = new Date();
var date = now.getDate();
var month = now.getMonth();
var Holidays = [
[8, 3],
[9, 8],
[10, 16],
[11, 7],
[11, 24],
[11, 25],
[11, 26],
[11, 27],
[11, 28],
[11, 29],
[11, 30],
[11, 31],
[0, 1],
[0, 2],
[0, 3],
[0, 4],
[0, 31],
[1, 15],
[1, 18],
[2, 11],
[2, 12],
[2, 13],
[2, 14],
[2, 15],
[2, 29],
[3, 1],
[4, 20],
[5, 26],
[5, 27],
[5, 28]
];
var i = 0;
while (i <= Holidays.length) {
if (check() === true) {
console.log("No school today.");
i = 32;
} else if (check() === false) {
if (i < Holidays.length) {
i++;
} else {
console.log("we work today.");
i++;
}
}
}
function check() {
if (month == Holidays[i][0] && date == Holidays[i][1]) {
return true;
} else {
return false;
}
}
The purpose is to make it print out "No school today" for the days in the holiday array, otherwise, it'd print out "we work today".
Whenever I run the script it always says
type error Holidays[i] undefined
Can someone help me with this?
At least one problem in your code
while (i <= Holidays.length) {
the last i should be Holidays.length-1, so use:
while (i < Holidays.length) {
The last time the code does an i++, i will end up with a value that is the last index of Holidays + 1.
So in the line that throws an error, you're trying to retrieve an item that's not in the array. You're not trying to get a specific element of the array and the offending code is not in a loop, so you might want to check that.
I have a problem with jQuery flot.
PHP output (not JSON):
[[1, 153], [2, 513], [3, 644]] ~~ [[1, 1553], [2, 1903], [3, 2680]]
jQuery call:
$.ajax({
url: 'xxx.php',
success: function (data) {
var dataArray = data.split('~~'),
dataArray1 = dataArray[0],
dataArray2 = dataArray[1],
plot = $.plot($('#xxx'), [{
data: dataArray1,
color: colours[0]
},
{
data: dataArray2,
color: colours[1],
points: {
show: true,
}
},
], {
series: {
bars: {
show: true,
barWidth: .6,
align: 'center'
}
},
grid: {
show: true,
hoverable: true,
clickable: true,
autoHighlight: true,
borderWidth: true,
borderColor: 'rgba(255, 255, 255, 0)'
},
xaxis: {
show: false
}
});
}
});
Taking the data in this way, I'm trying to use jQuery Flot but does not work...
Whereas, I can by separating data:
First label:
[[1, 153], [2, 513], [3, 644]]
Second label:
[[1, 1553], [2, 1903], [3, 2680]]
I'll share a Simple example jquery Flot with ajax for basic understanding purpose.
See this page and let change it into ajax : http://www.jqueryflottutorial.com/making-first-jquery-flot-line-chart.html
First, you must successful showing the chart as described without ajax. Don't forget to put height and width in div tag if you don't include css file.:
<div id="flot-placeholder" style="width: 100%; height: 260px"></div>
If ok, then follow this step.
STEP 1 : Put the script inside a function:
<script>
function show_chart(data) {
// this will be moved to php file
//var data = [[1, 130], [2, 40], [3, 80], [4, 160], [5, 159], [6, 370], [7, 330], [8, 350], [9, 370], [10, 400], [11, 330], [12, 350]];
var dataset = [{label: "line1",data: data}];
var options = {
series: {
lines: { show: true },
points: {
radius: 3,
show: true
}
}
};
$(document).ready(function () {
$.plot($("#flot-placeholder"), dataset, options);
});
}
</script>
STEP 2 : Create sample.php.
<?php
require 'config.php';
if($_POST)
{
$id = $_POST['id'];
$arr = array();
$arr = [[1, 130], [2, 40], [3, 80], [4, 160], [5, 159], [6, 370], [7, 330], [8, 350], [9, 370], [10, 400], [11, 330], [12, 350]];
echo json_encode($arr);
}?>
Note : $arr that moved from the first script then become only a sample data. You should make a php class or function that fetch data from database and return as array format as shown in $arr.
STEP 3 : Create simple ajax to get the response and render the chart :
var id = 1;
$.post('/..if any folder../sample.php', {
id : id,
}, function(response){
var data = JSON.parse(response);
show_chart(data); // call function and render the chart in <div id="flot-placeholder"></div>
});
Step Finish.
In some cases, we may need two or more data type. Then just add this to the code :
inside sample.php :
$arr1 = array();
$arr2 = array();
$arr1 = [[1, 130], [2, 40], [3, 80], [4, 160], [5, 159], [6, 370], [7, 330], [8, 350], [9, 370], [10, 400], [11, 330], [12, 350]];
$arr2 = [[1, 130], [2, 40], [3, 80], [4, 160], [5, 159], [6, 370], [7, 330], [8, 350], [9, 370], [10, 400], [11, 330], [12, 350]];
// put more if rquired
echo json_encode($arr1,$arr2); // put more array if required
inside ajax :
var data = JSON.parse(response);
var result1 = data[0]; // response data from $arr1
var result2 = data[1]; // response data from $arr2
Sorry for long description. Hope it will help.
Just for fun :
Some people don't want to show 'sample.php' in the console log. For this purpose we can simply change 'sample' as a folder and create index.php inside it and in the ajax we just direct the url to the folder like this :
$.post('/..if any folder../sample/'), { // this will open index.php
You have recieved a string that was not qualified as JSON data. Then you've splitted it on two strings, that are still not JSON. Then you trying to instantiate plot object with data: your_string_value. Here plot waiting of an object, not string.
Try to define data of your plot this way: data:$.parseJSON( dataArray1 )
I have a JSON array of data which is
[ [[2, 5], [6, 10], [10, 7], [11, 15]],
[[0, 9], [1, 16], [3, 19], [4, 15]],
[[0, 7], [5, 16], [8, 17], [12, 19]] ]
but when I try to get the first array of [[2, 5], [6, 10], [10, 7], [11, 15]] using jsonData[0] I get the data as 2,5,6,10,10,7,11,15.
I would like to get the data in the JSON format and not the plain text format. Any ideas?
You should get the data as an array. Did you alert jsonData[0] because that will display the results as a flattened string.
Instead console.log(jsonData[0]) to see the actual array.
Here's the output I see when using your array.
var a = [[[2, 5], [6, 10], [10, 7], [11, 15]],[[0, 9], [1, 16], [3, 19], [4, 15]],[[0, 7], [5, 16], [8, 17], [12, 19]]];
alert(a[0]); // 2,5,6,10,10,7,11,15
console.log(a[0]); // [[2, 5], [6, 10], [10, 7], [11, 15]]
See an example.
Also, "JSON raw format" is misleading. What you have is a plain JavaScript array.
Are you asking how to convert it to a JSON string rather than simply getting the default toString behaviour of an array? If so you should just do:
JSON.stringify(jsonData[0])
Or whatever it is you want to stringify