根据空格拆分字符串并在angular2中读取
Split string based on spaces and read that in angular2
我正在 angular2 中创建一个管道,我想在其中拆分字符串上的空格,然后将其作为数组读取。
let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);
当我记录这个时,我总是得到 "a" 作为输出。我哪里错了?
做了一些改动:
let stringToSplit = "abc def ghi";
let x = stringToSplit.split(" ");
console.log(x[0]);
split 方法returns一个数组。您不是使用其结果,而是获取原始字符串的第一个元素。
let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);
首先,stringToSplit
和StringToSplit
不一样。 JS 区分大小写。此外,您不会在任何地方保存 StringToSplit.split(" ")
的结果,然后您只输出字符串 stringToSplit
的第一个字符,即 a
。你可以这样做:
let stringToSplit = "abc def ghi";
console.log(stringToSplit.split(" ")[0]); // stringToSplit.split(" ") returns array and then we take the first element of the array with [0]
PS。它也比 TypeScript 或 Angular.
更多关于 JavaScript
我为它创建了这个 npm 包:https://www.npmjs.com/package/search-string-eerg
function customSearch(s, p) {
let x = p.split(" ");
var find = true;
for (var partIndex in x) {
if (s.toLowerCase().indexOf(x[partIndex]) > -1) {
// Let this item feature in the result set only if other parts of the
// query have been found too
find = find && true;
} else {
// Even if a single part of the query was not found, this item
// should not feature in the results
find = false;
}
}
return find;
}
我正在 angular2 中创建一个管道,我想在其中拆分字符串上的空格,然后将其作为数组读取。
let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);
当我记录这个时,我总是得到 "a" 作为输出。我哪里错了?
做了一些改动:
let stringToSplit = "abc def ghi";
let x = stringToSplit.split(" ");
console.log(x[0]);
split 方法returns一个数组。您不是使用其结果,而是获取原始字符串的第一个元素。
let stringToSplit = "abc def ghi";
StringToSplit.split(" ");
console.log(stringToSplit[0]);
首先,stringToSplit
和StringToSplit
不一样。 JS 区分大小写。此外,您不会在任何地方保存 StringToSplit.split(" ")
的结果,然后您只输出字符串 stringToSplit
的第一个字符,即 a
。你可以这样做:
let stringToSplit = "abc def ghi";
console.log(stringToSplit.split(" ")[0]); // stringToSplit.split(" ") returns array and then we take the first element of the array with [0]
PS。它也比 TypeScript 或 Angular.
更多关于 JavaScript我为它创建了这个 npm 包:https://www.npmjs.com/package/search-string-eerg
function customSearch(s, p) {
let x = p.split(" ");
var find = true;
for (var partIndex in x) {
if (s.toLowerCase().indexOf(x[partIndex]) > -1) {
// Let this item feature in the result set only if other parts of the
// query have been found too
find = find && true;
} else {
// Even if a single part of the query was not found, this item
// should not feature in the results
find = false;
}
}
return find;
}