将数组值与字符串进行比较

时间:2018-06-13 02:26:11

标签: javascript arrays string compare

let obj = {
	results: {
  	bindings: [
          { placeName: 'cali',  lat: '123',  long: '456' },
          { placeName: 'hawaii',  lat: '555',  long: '333'},
          { placeName: 'korea',  lat: '777',  long: '888' }]
        }
};

let markers = [];
obj.results.bindings.forEach(item => {
	let mar = { content: '', coords: {}};
  mar.content = item.placeName;
  mar.coords.lat = Number(item.lat);
  mar.coords.lng = Number(item.long);
  
  markers.push(mar)
})

console.log(markers)

如何将var string1 = "Hello, World"; var arr = string1.split(","); var string2 = "Hello World"; 数组的值与变量arr的值进行比较?

1 个答案:

答案 0 :(得分:1)

只需使用join方法,如下所示:

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

或者您可以尝试replace,如下所示:

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