Mei
Mei

Reputation: 31

Compare array value with string

var string1 = "Hello, World";
var arr = string1.split(",");
var string2 = "Hello World";

How do I compare the value of the arr array with the value of the variable string2?

Upvotes: 1

Views: 48

Answers (1)

Terry Wei
Terry Wei

Reputation: 1531

Just use join method as below:

var string1 = "Hello, World";
var arr = string1.split(",");
var string2 = "Hello World";
var arr_string = arr.join('');
console.log(arr_string == string2);

Or you could try replace as below:

var string1 = "Hello, World";
var string2 = "Hello World";
var new_string1 = string1.replace(/,/g, '');
console.log(new_string1 == string2);

Upvotes: 1

Related Questions