简体   繁体   English

如何将 object 作为属性添加到数组

[英]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.您好,我从 CSVparse function 获得以下数组。我试图直接从 JSON 获取我的数据,而不创建 csv 文件。

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:所以现在我不明白如何将以下属性 Colonne 添加到我的数组中:

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:我正在使用 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:然后我得到列的名称并可以建立我的 object 列:

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:所以目标是将这个 object 列放入我的数组中,如下所示:

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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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