我试图找到一种方法从数组中删除与给定模式匹配的一堆元素。
我的数组是:
var columns_array = [
"table_a.id",
"table_a.name",
"table_a.surname",
"table_b.user_id",
"table_b.dob",
"table_b.start_date"
]
现在我希望从该数组中删除与给定模式匹配的所有内容:
var rm_matching = "table_b"
以便生成的数组看起来像这样:
var columns_array = [
"table_a.id",
"table_a.name",
"table_a.surname"
]
我厌倦了拼接,地图和谷歌搜索提供的其他解决方案的少数变体(太多不能放在这里)但从未得到预期的结果。
我该如何处理?
答案 0 :(得分:0)
我会选择filter
功能:
var rm_matching = "table_b";
columns_array = columns_array.filter(
function(elem) {
return (!elem.match(rm_matching + "(.*)"));
});
答案 1 :(得分:-1)
使用var columns_array = [
"table_a.id",
"table_a.name",
"table_a.surname",
"table_b.user_id",
"table_b.dob",
"table_b.start_date"
]
var column_a = columns_array.filter(a => /table_a/.test(a));
var list = document.createElement("ul");
document.body.appendChild(list);
for (var i = 0; i < column_a.length; i++) {
var listItem = document.createElement("li");
listItem.innerHTML = column_a[i];
document.body.appendChild(listItem);
}
myorg_customer
&#13;
答案 2 :(得分:-1)
试试这个
var columns_array = [
"table_a.id",
"table_a.name",
"table_b.start_date",
"table_a.surname",
"table_b.start_date",
"table_b.user_id",
"table_a.surname",
"table_b.dob",
"table_b.start_date"
];
var str = "table_b"; // enter string you want to match
var newArr = Array();
var i = 0;
columns_array.forEach(function(item, index)
{
if (item.indexOf(str) == -1)
{
newArr [i] = item;
++i;
}
});
columns_array = newArr;
delete newArr;