Javascript array - split

javascript split regex
javascript split string at index
javascript splice
javascript slice
javascript split string by comma
javascript slice string
javascript substring
jquery split string to array

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

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

[ JavaScript ] Splice, Slice, Split - Jean Pan, Let's say that I have an Javascript array looking as following: ["Element 1","​Element 2","Element 3"  The split() method divides a String into an ordered set of substrings, puts these substrings into an array, and returns the array. The division is done by searching for a pattern; where the pattern is provided as the first parameter in the method's call.

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

Array.prototype.slice(), In this example the function split() creates an array of strings by splitting str wherever ” “ occurs. The second argument 2 limits the number of such splits to only 2. Basically, I pass in a string and an array of the values that the string will split on, and it will return an array of substrings, removing empty ones and including the values it splits on. If the string can be split at the same place by two different values, the longer one has precedence.

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

myVar = text.split(';');

like @Teemu, @Weedoze and @Alex said

Split array into chunks, Split an array into chunks in JavaScript. We have a large array and we want to split it into chunks. So these are few of the methods to do it. splice() This method  The slice() method returns a shallow copy of a portion of an array into a new array object selected from begin to end (end not included) where begin and end represent the index of items in that array. The original array will not be modified.

Something like this could do the trick:

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

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

JavaScript, That's why I call the second parameter “until”. let newArray = array.slice(0, 3); // Return value is Duration: 6:45 Posted: 9 Oct 2018 The splice() method adds/removes items to/from an array, and returns the removed item(s). Note: This method changes the original array.

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)

Split an array into chunks in JavaScript, JavaScript's string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or regular  The join () method returns the array as a string. The elements will be separated by a specified separator. The default separator is comma (,). Note: this method will not change the original array. Browser Support. The numbers in the table specify the first browser version that fully supports the method. array .join ( separator) Parameter Values.

splice( ), & split( ) methods in JavaScript, Creating array by breaking strings using delimiters in split function. Creating arrays by using split method in a string. Array Split <script type="text/javascript"​> JavaScript arrays are used to store multiple values in a single variable. An array is a special variable, which can hold more than one value at a time. If you have a list of items (a list of car names, for example), storing the cars in single variables could look like this: However, what if you want to loop through the cars and find a specific one?

Convert String to Array with JavaScript's String split Method, The best way to do what you want - iterate over an array and output it to the Document Object Model (DOM) - is to use a JavaScript template engine. JavaScript's string split method returns an array of substrings obtained by splitting a string on a separator you specify. The separator can be a string or regular expression. Invoke the split method on the string you want to split into array elements.

Split() method to create array by breaking string using delimiterin in , JavaScript | String split() str.split() function is used to split the given string into array of strings by separating it into substrings using a specified separator provided in the argument. The syntax of the function is as follows:

Comments
  • Just split all the arrays by semicolon too ..?
  • You already split the text by "\n". Why don't you try with ";" ?
  • I tried that but it gave me an error the method cant be used or something..
  • @TomášVítů glad to help
  • Use plain function then. Check my updated answer @TomášVítů
  • @TomášVítů glad to help