使用 Underscore.js 过滤列
Filter out columns using Underscore.js
我需要过滤我的数据结构以使用 underscore.js 生成图表。
var data = [
["Name", "Class", "Rollno", "Subject"],
["Anil", "10", "112121212", "BAS"],
["Sunil", "10", "112121212", "BAS"]
];
- 我的数据结构由行列表组成。
- 第一行包含每列的标签。
- 以下行包含图表的值。
因此,如果在这种情况下我将 Name
作为键传递,过滤器应该只删除第一列。此外,我想传入一个列名列表,以便从我的数据结构中删除多个列。
任何帮助将不胜感激。
Underscore 提供 "useful functional programming helpers" 并且是一个很棒的库,可以以更实用的方式对 JavaScript 进行编程。一些方法可能已经包含在现代 JavaScript.
代码使用了以下下划线方法:filter, each, map and contains
以下适用于单个列。
// Find the column index within the first row of the dataset
// using the given column name. This index will later be used in
// the filter.
//
// Take care, indexOf will return -1 if the column name was not found
// which would be a invalid index
var index = data[0].indexOf("Name");
// Generate a new dataset by collecting the filtered rows
// The input to the mapping function is each row.
var withoutColumn = _.map(data, function (row) {
// Apply a filter on each element in the row;
// The second parameter to the filter function is the current index
// of the element within the row.
return _.filter(row, function (r, idx) {
// Return true if the current index idx does not match the
// preselected index. This effectively removes the selected
// column.
return idx !== index;
});
});
现在让我们将它扩展到许多列:
a) 获取要删除的列的所有索引的列表。
b) 在过滤器中采用测试方法检查当前索引是否在先前找到的索引列表中。
function removeColumnsByName(data, columnNames) {
// Collect the index of each given column name in the array indexes
var indexes = [];
// Iterate over the column names
_.each(data[0], function(column, idx) {
// If the current column name is contained in the given column
// names store its index idx in indexes.
if (_.contains(columnNames, column)) {
indexes.push(idx);
}
});
// Again collect the filtered rows using a different filter function
return _.map(data, function (row) {
return _.filter(row, function (r, idx) {
// Modified test: return true if the current index idx is not
// contained in the list of indexes
return ! _.contains(indexes, idx);
});
});
}
console.log (removeColumnsByName(data, ["Name", "Subject"]));
要按列名删除行,您可以使用以下方法:
function filterByName (data, name) {
return _.filter(data, function (entry) {
return entry[0] !== name;
});
}
如果我没理解错的话,你想要这样的输出? :
[
["Class","Rollno","Subject"],
[ "10", "112121212","BAS"],
[ "10", "112121212","BAS"]
]
尝试做:
data.forEach(item => item.splice(0))
如果您想对任何列执行此操作,请使用:
data.forEach(item => item.splice(n)) // n is column index (zero based)
我需要过滤我的数据结构以使用 underscore.js 生成图表。
var data = [
["Name", "Class", "Rollno", "Subject"],
["Anil", "10", "112121212", "BAS"],
["Sunil", "10", "112121212", "BAS"]
];
- 我的数据结构由行列表组成。
- 第一行包含每列的标签。
- 以下行包含图表的值。
因此,如果在这种情况下我将 Name
作为键传递,过滤器应该只删除第一列。此外,我想传入一个列名列表,以便从我的数据结构中删除多个列。
任何帮助将不胜感激。
Underscore 提供 "useful functional programming helpers" 并且是一个很棒的库,可以以更实用的方式对 JavaScript 进行编程。一些方法可能已经包含在现代 JavaScript.
代码使用了以下下划线方法:filter, each, map and contains
以下适用于单个列。
// Find the column index within the first row of the dataset
// using the given column name. This index will later be used in
// the filter.
//
// Take care, indexOf will return -1 if the column name was not found
// which would be a invalid index
var index = data[0].indexOf("Name");
// Generate a new dataset by collecting the filtered rows
// The input to the mapping function is each row.
var withoutColumn = _.map(data, function (row) {
// Apply a filter on each element in the row;
// The second parameter to the filter function is the current index
// of the element within the row.
return _.filter(row, function (r, idx) {
// Return true if the current index idx does not match the
// preselected index. This effectively removes the selected
// column.
return idx !== index;
});
});
现在让我们将它扩展到许多列:
a) 获取要删除的列的所有索引的列表。
b) 在过滤器中采用测试方法检查当前索引是否在先前找到的索引列表中。
function removeColumnsByName(data, columnNames) {
// Collect the index of each given column name in the array indexes
var indexes = [];
// Iterate over the column names
_.each(data[0], function(column, idx) {
// If the current column name is contained in the given column
// names store its index idx in indexes.
if (_.contains(columnNames, column)) {
indexes.push(idx);
}
});
// Again collect the filtered rows using a different filter function
return _.map(data, function (row) {
return _.filter(row, function (r, idx) {
// Modified test: return true if the current index idx is not
// contained in the list of indexes
return ! _.contains(indexes, idx);
});
});
}
console.log (removeColumnsByName(data, ["Name", "Subject"]));
要按列名删除行,您可以使用以下方法:
function filterByName (data, name) {
return _.filter(data, function (entry) {
return entry[0] !== name;
});
}
如果我没理解错的话,你想要这样的输出? :
[
["Class","Rollno","Subject"],
[ "10", "112121212","BAS"],
[ "10", "112121212","BAS"]
]
尝试做:
data.forEach(item => item.splice(0))
如果您想对任何列执行此操作,请使用:
data.forEach(item => item.splice(n)) // n is column index (zero based)