从结果数组javascript中获取值

时间:2017-02-12 01:16:09

标签: javascript

Array[7998]
[0 … 99]
0:"        1    2015-01-14 13:27:07 1   0   2   0"
1:"        1    2015-01-14 13:30:11 1   0   2   0"
2:"        1    2015-01-14 13:33:34 1   0   2   0"
3:"        1    2015-01-14 13:33:43 1   0   2   0"
4:"        2    2015-01-21 10:54:37 1   0   1   0"
5:"        2    2015-02-03 11:39:57 1   0   1   0"
6:"        2    2015-02-03 11:44:56 1   4   1   0"
7:"        1    2015-02-03 11:44:59 1   4   2   0"
8:"        2    2015-02-03 11:49:49 1   0   1   0"
9:"      221    2015-02-03 11:49:51 1   0   1   0"
10:"      221   2015-02-03 11:49:53 1   0   1   0"
11:"      221   2015-02-03 11:52:07 1   0   1   0"
12:"        1   2015-02-26 17:29:24 1   4   2   0"

嗨!我从这段代码中得到了这个结果:

function onReadFile(file){
  var txt = file.target.result;
  var list = txt.split('\n');
//  var result = list.map(function(item){
//     return item.split('\n');
//  });
  console.log(list);
}

有没有办法只获得前三个值或列?前三列代表Id,日期和时间,所以我需要像这样提取三个..

[1,2015-01-14,13:27:07][1,2015-01-14,13:30:11][1,2015-01-14,13:33:34][...]

谢谢。

4 个答案:

答案 0 :(得分:1)

django-rest-framework-jwt

答案 1 :(得分:0)

您可以使用正则表达式将字符串拆分为多个空格。

var fields = list.split(/\s+/);
console.log(fields);

前三列分别位于索引1,2和3处。

答案 2 :(得分:0)

您可以拆分空格,然后将其映射到所需的值。

//the function that looks at the item entry and gets the 
//needed values.
function parse (item) {
    var items = item.split(' ')

  var actualItems = items.filter(x=>x !== "")

    return [
      actualItems[0], 
      actualItems[1],
      actualItems[2]
    ]
  }
}

//your array of items
var items = [
"        1    2015-01-14 13:27:07 1   0   2   0",
"        1    2015-01-14 13:30:11 1   0   2   0"
]

//transform your array to appropriate values.
var itemData = items.map(parse);

console.log(itemData)

答案 3 :(得分:0)

.match()不是空格字符的字符,.slice()结果数组的前三个元素

var list = ["        1    2015-01-14 13:27:07 1   0   2   0"
,"        1    2015-01-14 13:30:11 1   0   2   0"
,"        1    2015-01-14 13:33:34 1   0   2   0"
,"        1    2015-01-14 13:33:43 1   0   2   0"
,"        2    2015-01-21 10:54:37 1   0   1   0"
,"        2    2015-02-03 11:39:57 1   0   1   0"
,"        2    2015-02-03 11:44:56 1   4   1   0"
,"        1    2015-02-03 11:44:59 1   4   2   0"
,"        2    2015-02-03 11:49:49 1   0   1   0"
,"      221    2015-02-03 11:49:51 1   0   1   0"
,"      221   2015-02-03 11:49:53 1   0   1   0"
,"      221   2015-02-03 11:52:07 1   0   1   0"
,"        1   2015-02-26 17:29:24 1   4   2   0"];

var result = list.map(function(text) {
                   return text.match(/[^\s]+/g).slice(0, 3)
                 });

console.log(result);