Home > Blockchain >  How to convert JSON array to OBJECT array javascript?
How to convert JSON array to OBJECT array javascript?

Time:12-03

Kinda stuck here.

I am fetching data from database with php into this variable in javascript.

<?php
//connection to database
include("con.php");
//query
$query = "SELECT * FROM magacin_artikli";

$r = mysqli_query($conn, $query);

$dataGrafDodArt = array();

while($row = mysqli_fetch_array($r)){
  $dataGrafDodArt[] = $row["art_naz"]. ":". $row["art_nabcena"];

}

//closing conn
$conn->close();

?>
var oData = <?php echo json_encode($dataGrafDodArt);?>;

Output is:

Array (3)
1 "asd:2"
2 "asd:3"
3 "asd:2"
4 "ddd:3"

And I need this to be formated like object array like this("asd":2), something like this inside variable:

Example output:

var oData = {
  "2008": 10,
  "2009": 39.9,
  "2010": 17,
  "2011": 30.0,
  "2012": 5.3,
  "2013": 38.4,
  "2014": 15.7,
  "2015": 9.0
};

This is for animated graph which is taking parameters from Example output.

Any help would be good.

Tried a lot of things from array map to trimming the array and other stuff but none worked.

CodePudding user response:

You should make the change at the PHP side. Instead of building an indexed array, create an associative array, and it will look in JavaScript just as you wanted it.

Change this:

$dataGrafDodArt[] = $row["art_naz"]. ":". $row["art_nabcena"];

To:

$dataGrafDodArt[$row["art_naz"]] = $row["art_nabcena"];

CodePudding user response:

To convert a JSON array to an object array in JavaScript, you can use the map() method of the Array object to convert each element of the JSON array to an object. The map() method allows you to apply a function to each element of an array, and to return a new array containing the transformed elements.

For example, given the following JSON array:

[
  { "name": "John Doe", "age": 30 },
  { "name": "Jane Doe", "age": 25 }
]

You can use the map() method to convert the array to an object array, as follows:

const jsonArray = [
  { "name": "John Doe", "age": 30 },
  { "name": "Jane Doe", "age": 25 }
];

const objectArray = jsonArray.map(jsonObject => {
  return {
    name: jsonObject.name,
    age: jsonObject.age
  };
});

This code will convert each element of the JSON array to an object with the same name and age properties, and will return a new array containing these objects. The objectArray variable will then contain the following array:

[
  { "name": "John Doe", "age": 30 },
  { "name": "Jane Doe", "age": 25 }
]

Overall, to convert a JSON array to an object array in JavaScript, you can use the map() method of the Array object to transform each element of the JSON array into an object. This allows you to convert the JSON array into an array of objects with the desired properties and values.

CodePudding user response:

Try using JSON.parse(<jsondata>) This will parse the json into accepted javascript data structures such as objects and arrays. You can then modify to fit your needs

  • Related