逐行分隔数组
Separate array line by line
我从网页上抓取信息,我想将它们推送到数组以便以后使用它们。但是当我尝试到达第一个,第二个等等...项目时,我只得到一个字符而不是这个词。
var arrType = [];
$('[name="type"]> option').each(function () {
arrType.push($(this).text());
});
const vehicleType = arrType.join(", ");
如果我打印 vehicleType 然后我得到一些看起来像数组的东西(typeof 也是数组),但是当我想打印 vehicleType[0] 时我只得到一个字符。
console.log (vehicleType)
[text1,text2,text3]
console.log (vehicleType[0])
t
首先,您可以稍微减少代码。您可以在使用 .map()
而不是 .each()
.
拉取选项文本的同时定义变量
var arrType = $('[name="type"]> option').map(function() {
return $(this).text().trim();
}).toArray();
其次,要定位数组的第一项,根本不要使用 .join()
。
console.log(arrType[0]);
我从网页上抓取信息,我想将它们推送到数组以便以后使用它们。但是当我尝试到达第一个,第二个等等...项目时,我只得到一个字符而不是这个词。
var arrType = [];
$('[name="type"]> option').each(function () {
arrType.push($(this).text());
});
const vehicleType = arrType.join(", ");
如果我打印 vehicleType 然后我得到一些看起来像数组的东西(typeof 也是数组),但是当我想打印 vehicleType[0] 时我只得到一个字符。
console.log (vehicleType)
[text1,text2,text3]
console.log (vehicleType[0])
t
首先,您可以稍微减少代码。您可以在使用 .map()
而不是 .each()
.
var arrType = $('[name="type"]> option').map(function() {
return $(this).text().trim();
}).toArray();
其次,要定位数组的第一项,根本不要使用 .join()
。
console.log(arrType[0]);