1

I have a JSON array being passed form PHP and it looks like this:

[{from: "2019,09,14", to: "2019,09,14"}]

and I need it to look like this:

[{from: [2019, 9, 14], to: [2019, 9, 17]}]

here is my code thus far:

function getPricingDateRanges() { var city = document.getElementById("pricingcityselect").value;

$.ajax({
    url: 'getpricingdaterangepicker.php?city=' + city,       // Change this to the uri of your file
    method: 'GET',                 // Use the GET method
    dataType: 'json',              // Expect a JSON response
    success: function(response) {  // What will happen when the request succeeds

        var darray = [{from:[2019,9,14], to:[2019,9,17]}];

        console.log(response);
        console.log(darray);

    }
});

}

Added code from my PHP:

<?php
include 'dbconfig.php';

$city = ($_GET['city']);

$sql="SELECT start_date, end_date FROM date_ranges WHERE city='" . $city . "'";
$result = mysqli_query($conn,$sql);

// Create empty array.
$date_ranges = array();

// Add every row to array;
while($row = mysqli_fetch_array($result)) {

    // Create an associative array to store the values in.
    // This will later be a JavaScript Object.

    $from = new DateTime($row['start_date']);
    $ffrom = $from->format('Y,m,d');

    $to = new DateTime($row['end_date']);
    $fto = $from->format('Y,m,d');

    array_push($date_ranges, array(
        'from'   => $ffrom,
        'to'     => $fto
    ));

} 

// Send the $date_ranges as JSON.
$json = json_encode($date_ranges); // '[{"start": "2019-08-18", "end": "2019-08-19"}]'
echo $json;
?>

2 Answers2

3

First, it's important to point out that the "JSON" you've included is not JSON. It is neither a string, nor are the property names quoted.

However, if this is actually the input you're dealing with, you can loop through the items, split the from and to values on ,, and use .map(Number) to convert them to integers.

const arr = [{from: "2019,09,14", to: "2019,09,14"}];
 
//Helper function to split on comma, convert items to numbers
const toIntegerArray = str => str.split(",").map(Number);

const result = arr.map(({from,to}) => ({
  from: toIntegerArray(from),
  to: toIntegerArray(to)
}));

console.log(result);
Tyler Roper
  • 21,445
  • 6
  • 33
  • 56
  • Looks nice. Just curious, when can we substitute `el => parseInt(el)` by expected type `Number`? – Alex Vovchuk Oct 04 '19 at 01:10
  • 2
    Thanks Alex. You can read about the differences (better than I could explain) here: [What is the difference between parseInt() and Number()?](https://stackoverflow.com/questions/4090518/what-is-the-difference-between-parseint-and-number) In this case, `.map(Number)` is just shorthand for `.map(el => Number(el))`. – Tyler Roper Oct 04 '19 at 01:14
  • Yes, I got it. In this case we use Number as constructor function, that accepts string and returns number. Therefore it fits to callback declaration. Thanks! – Alex Vovchuk Oct 04 '19 at 01:18
  • While this works, and has the same console output as the other array, it is not working with my the date picker I am using. –  Oct 04 '19 at 04:10
  • You'll have to clarify. Your question said *"I have input that looks like A and I want it to look like B"*, so that's what I did. – Tyler Roper Oct 04 '19 at 12:53
1

Parse JSON till the point with "2019,09,14".

then "2019,09,14" convert to array of numbers by:

"2019,09,14".split(',').map(el => parseInt(el, 10)) 
Alex Vovchuk
  • 2,828
  • 4
  • 19
  • 40
  • You might want to add a `10` [_radix_](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt#Parameters) parameter to `parseInt`, otherwise the zero-padded numbers might be interpreted as octal – Phil Oct 04 '19 at 01:15