0
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"

Hi! I get this result from this code:

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

Is there a way to get just the first three values or columns? The first three columns represent the Id, date, and time so I need to extract just the three like so..

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

Thank you.

4
  • 1
    Are those spaces or tabs between each? Commented Feb 12, 2017 at 1:18
  • Show an example of the expected result (for 3 rows at least)? Do you want them as objects? Commented Feb 12, 2017 at 1:23
  • @ibrahimmahrir: please see my edit. Commented Feb 12, 2017 at 1:30
  • @Ibanez1408 Objects will be better! Check my answer! I used objects not arrays! Let me know if you want me to update it! Commented Feb 12, 2017 at 1:32

4 Answers 4

1

var text = "   1    2015-01-14 13:30:11 1   0   2   0\n        1    2015-01-14 13:33:34 1   0   2   0\n        1    2015-01-14 13:33:43 1   0   2   0";

var list = 
text.split('\n')                 // split the lines
    .map(function(line) {        // map the lines
      var parts = line.trim()    // trim the current line (remove surrounding spaces)
                  .split(/\s+/); // split the cols by multiple spaces (space, tabs, ...)

      return parts.slice(0, 3);  // return an array like your desired output (just the first three cols of each row)
      
      // IF YOU WANT THE RESULT TO BE AN OBJECT THEN REMOVE THE ABOVE RETURN STATEMENT AND UNCOMMENT THE CODE BELLOW

      //var o = {};
      //o.id = parts[0];
      //o.date = parts[1];
      //o.time = parts[2];

      //return o;
    });

console.log(list);

Sign up to request clarification or add additional context in comments.

3 Comments

Requirement is to return an array, not object.
@guest271314 See my comment under the question! I answered before he posted the desired output! So I'm now waiting for his response! This could be what he really wants!
Object is great! However, I need to place each line in a google sheet and the way to put it is through my sample output above. But, if you can teach me how to iterate through the objects to get to my sample above that would be a huge help!
0

You can split by a string using a regex for multiple white spaces.

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

And the first three columns are at index 1, 2 and 3, respectively.

Comments

0

.match() characters that are not space characters, .slice() first three elements of resulting array

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

Comments

0

You can split on the spaces and them map it to the desired values.

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

7 Comments

Requirement is to return an array, not object.
actually no, he's pretty clear in his response that an object of the values would be better.
"like so..[1,2015-01-14,13:27:07][1,2015-01-14,13:30:11][1,2015-01-14,13:33:34][...]"
Difficult to empirically determine what is "better". Only indicated actual text of requirement and javascript at original Question.
Ha! We both got beat out on that one, although you are clearly right, that is what he asked for.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.