简体   繁体   中英

How to add object as properties to array

Hi i get the following array from CSVparse function. I am trying to get my data directly from JSON without creating a csv file.

DATA (2) [{…}, {…}, columns: Array(4)]
0: {date: '2022-11-08 04:00:00', Ballon: '0.58', DepartPAC: '23.32', RetourPAC: '21.94'}
1: {date: '2022-11-08 04:01:00', Ballon: '0.59', DepartPAC: '23.98', RetourPAC: '21.58'}
columns: (4) ['date', 'Ballon', 'DepartPAC', 'RetourPAC']

So now i don't understand how i can add the following propertie Colonne to my array:

const Colonne = ['date', 'Ballon', 'DépartPAC', 'RetourPAC'];

As what i have tried is given an automatic index and i would like to have the string columns as an index (I know that index can be only numbers but it seems possible) as this is not added as an index but as a propertie. I get following result:

(2) [Array(77), {…}]
0:(77) [{…}, {…}, {…}, {…}, {…}, {…}]
1:{id: 'columns', values: Array(4)}

using this code:

 retour.push({columns:Colonne});
//or
retour.splice("Columns", 0, Colonne);

I am getting my data with AJAX:

$( document ).ready( function(){
    $.ajax({
        type: 'GET',
        url: 'DonneesGraph.php',
        dataType: 'json',
        success: function( retour ) {
            // Ici le traitement des données retournées placé dans quelquechose=JSON ? appelé retour 
            
            console.log( "Retour" , retour );

Then i get the name of columns and can buid my object columns:

const Lenght =Object.keys(retour[0]).length;
const Nom=Object.keys(retour[0]);
console.log("Nombre de colonnes",Lenght);
console.log("Nom",Nom[2]);
const Colonne = ['date', 'Ballon', 'DépartPAC', 'RetourPAC'];

So the goal is to get this object columns into my array same as this:

DATA (2) [{…}, {…}, columns: Array(4)]
0: {date: '2022-11-08 04:00:00', Ballon: '0.58', DepartPAC: '23.32', RetourPAC: '21.94'}
1: {date: '2022-11-08 04:01:00', Ballon: '0.59', DepartPAC: '23.98', RetourPAC: '21.58'}
columns: (4) ['date', 'Ballon', 'DepartPAC', 'RetourPAC']

Hope i am clear enough if my explaination. Thanks

I didn't understand your question completely. But I think you're trying to achieve something like this.

{
  date: [ '2022-11-08 04:00:00', '2022-11-08 04:01:00' ],
  Ballon: [ '0.58', '0.59' ],
  DepartPAC: [ '23.32', '23.98' ],
  RetourPAC: [ '21.94', '21.58' ]
}

Here's the code for that.

let data = [
    {
        date: '2022-11-08 04:00:00', 
        Ballon: '0.58', 
        DepartPAC: '23.32', 
        RetourPAC: '21.94'
    }, 
    {
        date: '2022-11-08 04:01:00', 
        Ballon: '0.59', 
        DepartPAC: '23.98', 
        RetourPAC: '21.58'
    }];

function getColumnWiseData(data) {
    let columnWiseData = {};
    let columns = Object.keys(data[0]);
    columns.forEach((column) => {
        columnWiseData[column] = [];
    });
    data.forEach((row) => {
        columns.forEach((column) => {
            columnWiseData[column].push(row[column]);
        });
    });
    return columnWiseData;
}

console.log(getColumnWiseData(data));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM