从重复的javascript数组结果集中只获取一行
Getting just one row from a duplicate javascript array result set
我有以下文本文件stops.txt
。
stop_id,stop_code,stop_name,stop_lat,stop_lon,zone_id,stop_url,location_type,parent_station,platform_code,wheelchair_boarding
70011,70011,San Francisco Caltrain,37.77639,-122.394992,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,NB,1
70012,70012,San Francisco Caltrain,37.776348,-122.394935,1,http://www.caltrain.com/stations/sanfranciscostation.html,0,ctsf,SB,1
70021,70021,22nd St Caltrain,37.757599,-122.39188,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,NB,2
70022,70022,22nd St Caltrain,37.757583,-122.392404,1,http://www.caltrain.com/stations/22ndstreetstation.html,0,ct22,SB,2
以下是我的javascript函数,用于获取datalist
:的stop_name索引
get('../data/stops.txt').then(function(response) {
//console.log("Success!", response.split(/('r'n|'n)/));
var stop_list = response.split(/('r'n|'n)/);
var re = /,/;
var headers = stop_list.shift().split(re);
var index = headers.indexOf("stop_name");
//Removing [index] at return val.split(re)[index]; should return an array of arrays of each split value
var res = stop_list.map(function(val, key) {
return val.split(re)[index];
}).filter(Boolean);
var str = '';
var i;
for (i = 0; i < res.length; i++) {
str += '<option value="'+res[i]+'" />';
}
var s_list=document.getElementById("slist");
s_list.innerHTML = str;
}, function(error) {
console.error("Failed!", error);
});
这简直太完美了。但是由于第一行具有与第2行相同的站点名称。
如果stop_name和plate_form相同,我想要的是只得到一个stop_name的结果集。
在定义res
以删除重复值时,您所需要做的就是在链的末尾添加一个过滤器。这个问题已经解决了好几次:数组中的唯一值
它看起来是这样的:
var res = stop_list
.map(function(val, key) {
return val.split(re)[index];
})
.filter(function(value, index, self) {
return self.indexOf(value) === index;
});
我喜欢雨果的回答。我只想补充几条,因为你也想验证这个平台的独特性。
var stop_list = response.split(/('r'n|'n)/);
var re = /,/;
var headers = stop_list.shift().split(re);
var index = headers.indexOf("stop_name");
var platIndex = headers.indexOf("platform_code");
var res = stop_list
.map(function(val, key) {
return val.split(re)[index] ? val.split(re)[index] + ',' + val.split(re)[platIndex] : '';
})
.filter(function(value, index, self) {
return value && self.indexOf(value) === index;
});
var str = '';
var i;
for (i = 0; i < res.length; i++) {
str += '<option value="'+res[i].split(re)[0]+'" />';
}
console.log(str);
//str: <option value="San Francisco Caltrain" /><option value="22nd St Caltrain" /><option value="22nd St Caltrain" />
所有的platform_code对于每个stop_name都是唯一的,所以我将前两个都改为使用NB来获得上面的输出。
相关文章:
- 从重复的javascript数组结果集中只获取一行
- 文本区域-获取每一行,找到换行符
- 获取DB表上每一行的值
- 如何在从服务器获取数据的动态表的每一行中添加下拉列表
- Javascript定期获取mysql的最后一行
- 仅获取具体化选择中的最后一行
- 如何使用 jQuery 获取一行中的列
- PHP 脚本仅从 MySQL 数据库中获取地址字段的第一行
- 如何使用 Angular.js 获取表的每一行数据
- 如何获取 jqgrid 中一行的格式化单元格值的值
- Javascript - 获取一行中的最后一个单词
- 使用 jQuery 获取表的最后一行
- 从同一行中获取最接近复选框列的列的 html 值
- jQuery从文本文件中随机获取一行
- Jquery-获取一行的所有内容
- 如何获取一行并将该行和文本字符串合并为一个新行
- 获取一行jQuery上的最后一个li
- NODE.js和Javascript;从serialPort获取一行,然后关闭
- 从网格ExtJs 4中获取一行
- 获取一行中当前选择元素之后的所有选择