用于数组中的循环数组。

时间:2021-11-03 21:26:57

I need to create a variable which store array in array by for loop to pass chart data to a chart.

我需要创建一个变量,该变量将数组中的数组用于循环,以将图表数据传递给图表。

Consider I have some JSON data and I am trying to acquire all the data with the below process

考虑一下我有一些JSON数据,我正在尝试用下面的过程获取所有的数据。

var FansChart = [];
    for (var i = 0; i < 3 ; i++) {
        for (var j = 0; j < 33; j++) {
             FansChart.push({
                name : url[i],
                data : data.result[i].trendData[j].value, //api thing doesn't really matter
              });
            }
        }

but it seem I am using the wrong method, how can I correct this?

但似乎我用错了方法,我该怎么改正呢?

I wish to be able to call something like:

我希望能打电话:

data.result[2].trendData[23].value

UPDATE one:

更新:

var abc =[];
            var FansChart = [];
            for (var i = 0; i < result.length; i++) {
                    for (var j = 0; j < data.result[0].trendData.length; j++) {
                        abc.push(data.result[i].trendData[j].value);
                    }
                  FansChart.push({
                    name : url[i],
                    data : abc

                  });
                  }

I have update my method like above is more like what i want to do, but i realize that all the trendDate have been bonded to varaible abc, how may i separate them?

我已经更新了我的方法,就像我想做的一样,但是我意识到所有的trendDate都和varaible abc绑定在一起,我要怎么把它们分开呢?

JSON

JSON

errorCode: null,
message: "Success",
result: [{
    urlId: "a279ab1177ab8d7011466e77b79b98f7",
    trendData: [],
    url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}, {
    urlId: "a279ab1177ab8d7011466e77b79b98f7",
    trendData: [{
        date: "2016-06-04",
        value: 0
    }, {
        date: "2016-06-21",
        value: 0
    }, {
        date: "2016-06-12",
        value: 0
    }, {
        date: "2016-06-05",
        value: 0
    }, {
        date: "2016-06-07",
        value: 0
    }, {
        date: "2016-06-19",
        value: 0
    }, {
        date: "2016-06-25",
        value: 1527
    }, {
        date: "2016-06-25",
        value: 0
    }, {
        date: "2016-06-11",
        value: 0
    }, {
        date: "2016-06-15",
        value: 0
    }, {
        date: "2016-06-08",
        value: 0
    }, {
        date: "2016-06-27",
        value: 1527
    }, {
        date: "2016-06-27",
        value: 0
    }, {
        date: "2016-06-06",
        value: 0
    }, {
        date: "2016-06-29",
        value: 0
    }, {
        date: "2016-06-09",
        value: 0
    }, {
        date: "2016-06-03",
        value: 0
    }, {
        date: "2016-06-17",
        value: 0
    }, {
        date: "2016-06-24",
        value: 1527
    }, {
        date: "2016-06-24",
        value: 0
    }, {
        date: "2016-06-13",
        value: 0
    }, {
        date: "2016-06-22",
        value: 0
    }, {
        date: "2016-06-01",
        value: 0
    }, {
        date: "2016-06-14",
        value: 0
    }, {
        date: "2016-06-18",
        value: 0
    }, {
        date: "2016-06-26",
        value: 1527
    }, {
        date: "2016-06-26",
        value: 0
    }, {
        date: "2016-06-20",
        value: 0
    }, {
        date: "2016-06-10",
        value: 0
    }, {
        date: "2016-06-23",
        value: 0
    }, {
        date: "2016-06-28",
        value: 0
    }, {
        date: "2016-06-02",
        value: 0
    }, {
        date: "2016-06-16",
        value: 0
    }],
    url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}, {
    urlId: "a279ab1177ab8d7011466e77b79b98f7",
    trendData: [{
        date: "2016-06-04",
        value: 0
    }, {
        date: "2016-06-21",
        value: 0
    }, {
        date: "2016-06-12",
        value: 0
    }, {
        date: "2016-06-05",
        value: 0
    }, {
        date: "2016-06-07",
        value: 0
    }, {
        date: "2016-06-19",
        value: 0
    }, {
        date: "2016-06-25",
        value: 1527
    }, {
        date: "2016-06-25",
        value: 0
    }, {
        date: "2016-06-11",
        value: 0
    }, {
        date: "2016-06-15",
        value: 0
    }, {
        date: "2016-06-08",
        value: 0
    }, {
        date: "2016-06-27",
        value: 1527
    }, {
        date: "2016-06-27",
        value: 0
    }, {
        date: "2016-06-06",
        value: 0
    }, {
        date: "2016-06-29",
        value: 0
    }, {
        date: "2016-06-09",
        value: 0
    }, {
        date: "2016-06-03",
        value: 0
    }, {
        date: "2016-06-17",
        value: 0
    }, {
        date: "2016-06-24",
        value: 1527
    }, {
        date: "2016-06-24",
        value: 0
    }, {
        date: "2016-06-13",
        value: 0
    }, {
        date: "2016-06-22",
        value: 0
    }, {
        date: "2016-06-01",
        value: 0
    }, {
        date: "2016-06-14",
        value: 0
    }, {
        date: "2016-06-18",
        value: 0
    }, {
        date: "2016-06-26",
        value: 1527
    }, {
        date: "2016-06-26",
        value: 0
    }, {
        date: "2016-06-20",
        value: 0
    }, {
        date: "2016-06-10",
        value: 0
    }, {
        date: "2016-06-23",
        value: 0
    }, {
        date: "2016-06-28",
        value: 0
    }, {
        date: "2016-06-02",
        value: 0
    }, {
        date: "2016-06-16",
        value: 0
    }],
    url: "https://www.instagram.com/p/BFQGk2WER56/?taken-by=fishfung_saimei&hl=en"
}],

4 个解决方案

#1


0  

You're missing a semicolon at the 2nd line. It should be for (var i = 0; i < 3; i++) {

你在第二行漏掉了一个分号。应该是(var i = 0;我< 3;我+ +){

#2


0  

Here you go..

给你. .

FansChart.push({
            name : url[i],
            data : data.result[i].trendData[j].value // no comma here
          });

Remove the comma in data : data.result[i].trendData[j].value statement.

删除数据中的逗号:data.result[i]. trenddata [j]。值语句。

#3


0  

Convert i to j in for :

将i转换为j:

Change :

变化:

for (var j = 0; j < 33; i++) {

to :

:

for (var j = 0; j < 33; j++) {

#4


0  

Remove i++ from second for loop

从第二个for循环中删除i++。

var FansChart = [];
for (var i = 0; i < 3 ; i++) {
    for (var j = 0; j < 33; j++) {
         FansChart.push({
            name : url[i],
            data : data.result[i].trendData[j].value //api thing doesn't really matter
          });
        }
    }

I hope it will work for you.

我希望它对你有用。

#1


0  

You're missing a semicolon at the 2nd line. It should be for (var i = 0; i < 3; i++) {

你在第二行漏掉了一个分号。应该是(var i = 0;我< 3;我+ +){

#2


0  

Here you go..

给你. .

FansChart.push({
            name : url[i],
            data : data.result[i].trendData[j].value // no comma here
          });

Remove the comma in data : data.result[i].trendData[j].value statement.

删除数据中的逗号:data.result[i]. trenddata [j]。值语句。

#3


0  

Convert i to j in for :

将i转换为j:

Change :

变化:

for (var j = 0; j < 33; i++) {

to :

:

for (var j = 0; j < 33; j++) {

#4


0  

Remove i++ from second for loop

从第二个for循环中删除i++。

var FansChart = [];
for (var i = 0; i < 3 ; i++) {
    for (var j = 0; j < 33; j++) {
         FansChart.push({
            name : url[i],
            data : data.result[i].trendData[j].value //api thing doesn't really matter
          });
        }
    }

I hope it will work for you.

我希望它对你有用。