Javascript array - split

I have a text file in which I have data on every line. It looks like this:

number0;text0
number1;text1
number2;text2

..and so on

So I loaded that text file into a variable via xmlhttprequest and then I converted it into an array using split by "\n" so now the result of lineArray[0] is number0;text0.. And now what I need is to split that array again so I could use number0 and text0 separately.

My idea being that I want to get the text0 by searching number0 for example lineArray[i][1] gives me texti..

Any ideas how to proceed now?

Thanks

Upvotes: 1

Views: 105

Answers (5)

Nick Parsons
Nick Parsons

Reputation: 50749

Something like this could do the trick:

let a = "number0;text0\nnumber1;text1\nnumber2;text2";
let lines = a.split('\n');

let vals = [];
for(let line of lines) {
  vals.push(line.split(';'))
}
console.log(vals); // Output

The last four lines create an empty array and split on the ';' and append that value to the vals array. I assume you already have the something like the first 2 lines

Upvotes: 0

Ankit Agarwal
Ankit Agarwal

Reputation: 30739

You need to do an additional split on ; as split(';') so that lineArray[0][1], lineArray[1][1] and so on gives you text0, text1 and so on.

var str = `number0;text0
number1;text1
number2;text2`;
var lineArray = str.split('\n').map(function(item){
   return item.split(';');
});
console.log(lineArray);
console.log(lineArray[0][1]);
console.log(lineArray[1][1]);

Upvotes: 1

Ivan
Ivan

Reputation: 40658

Convert the array into an object

Make an object out of it with another String split. To do so you can use the .reduce method to convert the array of strings into an object.

const strings = ['number0;text0', 'number1;text1', 'number3;text3', 'number4;text4'] ;

const obj = strings.reduce((acc,curr) => {
  const [key, value] = curr.split(';');
  acc[key] = value;
  return acc;
}, {});

console.log(obj)

This way you can access text4 buy calling obj['number4'].


More about .reduce

The reduce method works by looping through strings

  • on each step acc is the accumulator: it contains the object that is getting filled with key/value pairs.

  • cur is the current item in the step

  • const [key, value] = curr.split(';') will to split the string into two strings and assign each to a seperate variable: key and value. It's called destructuring assignment

  • then I assign the key/value pair to the accumulator

  • the .reducemethod will return the accumulator on his state on the last step of the loop

Upvotes: 0

Alex
Alex

Reputation: 2232

Knowing I'm late, still making a contribution.

As everyone else said, split it again.

let text = "number0;text0\nnumber1;text1\nnumber2;text2"
let data = text.split('\n');
var objects = {};

for (var i = 0; i < data.length; i++) {

  let key = data[i].split(';')[0]; // Left hand value [Key]
  let value = data[i].split(';')[1]; // Right hand value [Value]
  
  // Add key and value to object
  objects[key] = value;
}

// Access by property
console.log(objects);


Using forEach

let text = "number0;text0\nnumber1;text1\nnumber2;text2"
let data = text.split('\n');
var objects = {};

data.forEach((elem) => {

  let key = elem.split(';')[0]; // Left hand value [Key]
  let value = elem.split(';')[1]; // Right hand value [Value]
  
  objects[key] = value;
});
// Access by property
console.log(objects);

Upvotes: 0

Matheus Pitz
Matheus Pitz

Reputation: 476

Just use split again with ";", like that:

myVar = text.split(';');

like @Teemu, @Weedoze and @Alex said

Upvotes: 0

Related Questions