简体   繁体   English

如何提取数组的元素

[英]How to extract elements of an Array

I have the above data , 我有以上数据,

var jsonData = [
    {date:'August 19, 2004',open:100.01,high:104.06,low:95.96,close:100.34,volume:22088000},
    {date:'August 20, 2004',open:101.48,high:109.08,low:100.50,close:108.31,volume:11377000},
    {date:'August 23, 2004',open:110.76,high:113.48,low:109.05,close:109.40,volume:9090700},
    {date:'August 24, 2004',open:111.24,high:111.60,low:103.57,close:104.87,volume:7599100},
    {date:'August 25, 2004',open:104.96,high:108.00,low:103.88,close:106.00,volume:4565900}
];

I would like to get all the sigle values of the date , i have used this one 我想获取日期的所有sigle值,我已经使用了这个

for(var i = 0; i<jsonData.length; i++) 
    var date = jsonData[i].date;
date = date.split(' ');
return date; 

But i am getting only the Last Value that is August,25,,2004 但是我只得到了2004年8月25日的最后价值

How can i get all the values ?? 我如何获得所有值?

In your loop you are putting all the dates in the same variable, so in each iteration you are overwriting the previous value. 在循环中,您将所有日期都放在同一个变量中,因此在每次迭代中,您都将覆盖先前的值。 Add the dates to an array instead: 而是将日期添加到数组中:

var dates = [];
for(var i = 0; i<jsonData.length; i++) {
  dates.push(jsonData[i].date);
}
return dates; 

That returns an array of date strings. 这将返回一个日期字符串数组。 If you want to split each date and return an array of array of strings: 如果要拆分每个日期并返回一个字符串数组数组:

var dates = [];
for(var i = 0; i<jsonData.length; i++) {
  dates.push(jsonData[i].date.split(' '));
}
return dates; 

There is a problem with your loop. 您的循环有问题。 It only runs once, and even if you removed that, it would keep overwriting the value stored in date 它只运行一次,即使删除了它,它也会继续覆盖存储在 date的值

If you want to store all the dates, you need to make it into an array and store the date. 如果要存储所有日期,则需要将其放入数组并存储日期。 I'm not sure what you mean by single values of dates but this structure will solve the problem for you. 我不确定single values of datessingle values of dates什么意思,但是这种结构可以为您解决问题。

var dates = [];
for(var i = 0; i<jsonData.length; i++) {
   var date = jsonData[i].date;
   date = date.split(' '); 
   //do whatever you want with your date. transform.
   dates.push(date); //push the final version of the date you want to store
}
return dates;  //return an array of dates

You are overwriting the date variable in each iteration of your loop. 您将在循环的每次迭代中覆盖date变量。 Instead, keep a variable (initialized outside of the loop) and push each date array (generated inside the loop) to it. 相反,保留一个变量(在循环外部初始化)并将每个日期数组(在循环内部生成)压入该变量。

dates = [];
for ( idx in jsonData ) {
    dates.push(jsonData[idx].split(' '));
}

// dates is an array of arrays now
// for example: [['August', '19,', '2004'], ...]
return dates;

hi here you define a loop so every value of array is overwrite in date variable so it gives last overwritten values 嗨,在这里您定义了一个循环,以便数组的每个值都在日期变量中被覆盖,因此它给出了最后一个被覆盖的值

to extract all value you do not use loop this type you define 提取所有不使用的值,请循环定义此类型

you simply write 你只要写

var date1 = jsonData[0].date; var date1 = jsonData [0] .date; var date2 = jsonData[1].date; var date2 = jsonData [1] .date;

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

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