我在javascript中有一个可观察的数组,
lookups.Data
[...]
__proto__: []
length: 2884
[0]: {...}
[1]: {...}
[2]: {...}
[3]: {...}
[4]: {...}
[5]: {...}
[6]: {...}
[7]: {...}
[8]: {...}
[9]: {...}
[10]: {...}
每个记录都像,
lookups.Data[0]
{...}
__proto__: {...}
BookName: "Silapathikaram"
Author: "Illangovadigal"
type: "Tamil"
我有一个someBooks
值的数组,
someBooks = ["ABC", "CED", "Silapathikaram"]
现在我需要从不在查找数据中的某些书中删除书籍。
如果是单个数组,单个项目我可以使用indexof like,
lookup.indexOf(book) == -1 // to check the existance of the book in the lookup
任何人都可以建议我有没有有效或简单的方法来做到这一点?任何帮助都会非常有用。
答案 0 :(得分:1)
您可以在条件Array#filter功能中尝试Array#includes功能。
const someBooks = ['A', 'B', 'C'];
const lookup = ['A', 'C'];
const filteredBooks = someBooks.filter(book => lookup.includes(book));
console.log(filteredBooks);

答案 1 :(得分:1)
最有效的方法是使用一套。构建一组所有书名,并找到名称和someBooks
的交集。
let allNames = new Set(lookups.Data.map(e => e.BookName)),
someBooks = new Set(["ABC", "CED", "Silapathikaram"]);
let intersection = new Set();
for (var elem of allNames) {
if (someBooks.has(elem)) {
intersection.add(elem);
}
}
多次对整个书名列表执行.includes
会很慢且效率低下,而使用一组不应该有这个问题,但最终还是取决于浏览器的实现。
答案 2 :(得分:1)
为了过滤func scrollViewWillEndDragging(_ scrollView: UIScrollView, withVelocity velocity: CGPoint, targetContentOffset: UnsafeMutablePointer<CGPoint>) {
if(velocity.y>0) {
//Code will work without the animation block.I am using animation block incase if you want to set any delay to it.
UIView.animate(withDuration: 1.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.searchBar.isHidden = true
print("Hide")
}, completion: nil)
} else {
UIView.animate(withDuration: 1.5, delay: 0, options: UIViewAnimationOptions(), animations: {
self.searchBar.isHidden = false
print("Unhide")
}, completion: nil)
}
}
数组中不存在的某些内容的值,您可以使用lookups.Data
和some
函数
filter