javascript - 如果字符串存在于javascript中的其他元素中,我想从字符串数组中删除一个元素
问题描述
我希望能够删除数组中的元素,如果它出现在或包含在同一数组中的任何其他元素中。
例如:
const arr = ["Hello" , "Hell" , "Lower" , "Low", "Hii"];
目标输出:
["Hello" , "Lower", "Hii]
解决方案
使用数组Array.prototype.find()、Array.prototype.includes() 和Set的一种方法,
const arr = ["Hello", "Hell", "Lower", 'Lo', "Low", "Hii"];
const expected = [];
arr.forEach(function(element) {
el = arr.find(a => a.includes(element));
expected.push(el);
})
console.log([...new Set(expected)]);
推荐阅读
- apache-pig - 如何检查元组是否包含 Apache Pig 中的元素?
- r - 检验假设 R 中今年的通过率是否显着低于 0.5?
- java - 如何在 SpringBoot 中制作可以重定向到 url 的 REST api
- python - 将 pandas 数据框转换为字典,其中一列键和另一列值
- sql - 过去 n 天的 Redshift SQL 事件数
- r - 是否有 R 函数可以在 2 个条件下使用 ifelse
- spring-boot - 有没有办法使用 jdbctemplate 将从文件中读取的 sql 批量插入到数据库中?
- django - Django 电子商务:未找到带有参数 '('',)' 的 'cart_add' 的反向。尝试了 1 种模式:['cart/add/(?P
[0-9]+)/$'] - python - Pandas:用 0 填充一些分类数据
- c - 有效地找到大数组中的最低有效位?