下划线 return 数组中对象的索引,其中单词存在于对象内的句子中
Underscore return indeces of objects in an array where word exists in sentences within objects
我有一个数组,里面有这样的数组;
var lines = [
["1","1","1","A man is walking."]
,["1","1","2","Noooo he's not, no way!"],
["1","1","3","You can't see that far can you?"],
["1","1","4","I can, he stopped, he's looking right at us"]
];
如果 line[4] 正好是搜索语句,使用 Underscore 我可以在 "lines" 中得到一个数组,例如,"A man is walking." 会 return lines[0];
所以我希望能够只用一个词来搜索这些句子(行),比如 "Walking" 应该匹配 return 'lines' 中的第一个数组因为有一个句子包含这个词。
_.some(lines, function(array){
var result = (array[4] == 'walking' && array[4]);
if (result !== false){
console.log(result);
}
});
我该如何修改这个下划线函数,或者如果有一个我应该使用的正确的函数,或者如果有的话,即使它没有下划线,也请提出建议。
_.some
returns 布尔值。您需要 filter
通过查看搜索词是否在字符串中获得的匹配结果。索引从 0
开始,因此您需要检查索引 3 而不是 4。
工作示例:
var lines = [
["1", "1", "1", "A man is walking."],
["1", "1", "2", "Noooo he's not, no way!"],
["1", "1", "3", "You can't see that far can you?"],
["1", "1", "4", "I can, he stopped, he's looking right at us"]
];
var input = document.getElementById('search');
var output = document.getElementById('output');
input.onkeyup = function (event) {
var value = this.value;
var results = _.filter(lines, function (array) {
return array[3].indexOf(value) > -1;
});
var indexes = _.map(results, function(array) {
return lines.indexOf(array);
});
output.innerHTML = '<pre>Indexes: ' + JSON.stringify(indexes) + '</pre><pre>' + JSON.stringify(results, null, 2) + '</pre>';
};
<script src="//cdn.jsdelivr.net/lodash/2.1.0/lodash.compat.js"></script>
<input type="text" id="search" placeholder="Search">
<output id="output"></output>
假设你没有 ES6 的奢侈,简单地说 javascript:
var lines = [
["1","1","1","A man is walking."],
["1","1","2","Noooo he's not, no way!"],
["1","1","3","You can't see that far can you?"],
["1","1","4","I can, he stopped, he's looking right at us"]
];
function lineSearch(arr, term) {
var indices = arr.map(function(innerArr, index) {
return innerArr[3].indexOf(term) > -1 ? index : null;
}).filter(function(x) {
return x !== null;
});
var results = arr.map(function(innerArr, index) {
return innerArr[3].indexOf(term) > -1 ? innerArr : null;
}).filter(function(x) {
return x !== null;
});
return {indices: indices, results: results};
}
console.log(lineSearch(lines, "can"));
应该给:
{
indices: [2, 3],
results: [["1", "1", "3", "You can't see that far can you?"], ["1", "1", "4", "I can, he stopped, he's looking right at us"]]
}
我有一个数组,里面有这样的数组;
var lines = [
["1","1","1","A man is walking."]
,["1","1","2","Noooo he's not, no way!"],
["1","1","3","You can't see that far can you?"],
["1","1","4","I can, he stopped, he's looking right at us"]
];
如果 line[4] 正好是搜索语句,使用 Underscore 我可以在 "lines" 中得到一个数组,例如,"A man is walking." 会 return lines[0];
所以我希望能够只用一个词来搜索这些句子(行),比如 "Walking" 应该匹配 return 'lines' 中的第一个数组因为有一个句子包含这个词。
_.some(lines, function(array){
var result = (array[4] == 'walking' && array[4]);
if (result !== false){
console.log(result);
}
});
我该如何修改这个下划线函数,或者如果有一个我应该使用的正确的函数,或者如果有的话,即使它没有下划线,也请提出建议。
_.some
returns 布尔值。您需要 filter
通过查看搜索词是否在字符串中获得的匹配结果。索引从 0
开始,因此您需要检查索引 3 而不是 4。
工作示例:
var lines = [
["1", "1", "1", "A man is walking."],
["1", "1", "2", "Noooo he's not, no way!"],
["1", "1", "3", "You can't see that far can you?"],
["1", "1", "4", "I can, he stopped, he's looking right at us"]
];
var input = document.getElementById('search');
var output = document.getElementById('output');
input.onkeyup = function (event) {
var value = this.value;
var results = _.filter(lines, function (array) {
return array[3].indexOf(value) > -1;
});
var indexes = _.map(results, function(array) {
return lines.indexOf(array);
});
output.innerHTML = '<pre>Indexes: ' + JSON.stringify(indexes) + '</pre><pre>' + JSON.stringify(results, null, 2) + '</pre>';
};
<script src="//cdn.jsdelivr.net/lodash/2.1.0/lodash.compat.js"></script>
<input type="text" id="search" placeholder="Search">
<output id="output"></output>
假设你没有 ES6 的奢侈,简单地说 javascript:
var lines = [
["1","1","1","A man is walking."],
["1","1","2","Noooo he's not, no way!"],
["1","1","3","You can't see that far can you?"],
["1","1","4","I can, he stopped, he's looking right at us"]
];
function lineSearch(arr, term) {
var indices = arr.map(function(innerArr, index) {
return innerArr[3].indexOf(term) > -1 ? index : null;
}).filter(function(x) {
return x !== null;
});
var results = arr.map(function(innerArr, index) {
return innerArr[3].indexOf(term) > -1 ? innerArr : null;
}).filter(function(x) {
return x !== null;
});
return {indices: indices, results: results};
}
console.log(lineSearch(lines, "can"));
应该给:
{
indices: [2, 3],
results: [["1", "1", "3", "You can't see that far can you?"], ["1", "1", "4", "I can, he stopped, he's looking right at us"]]
}