【发布时间】:2016-10-11 12:58:31
【问题描述】:
我有两个数组:
//list elements from html
var list = document.querySelectorAll('#pres-list-ul li');
//list of retrieved objects from api
var objects = self.retrievedItems;
为了提高效率,列表的内容保存在一个html文件中(除非你刷新,否则不需要重新渲染相同的数据)
如果检索到的对象中不再存在列表项,我想从 html 中删除它。
我知道它在下面的代码行中,但我无法解决。
//I need to compare the id? For that another loop - which will run hundreds and thousands of time?
for(var j = 0; j < objects.length; j++) {
if(objects.indexOf(list[i]) === -1) {
//false
} else {
//true
}
}
场景:
list: 57 //local
objects: 56 //online
在列表中找到多余的值并将其删除。
列表:
<li id="item-5756" data-category="" data-group="" data-created="" data-author=""></li>
对象:
0: {
id: //
title: //
description //
// ...
}
1: {
//...
}
【问题讨论】:
-
您要查找两个列表的交集吗?每个列表中的项目是否唯一?
-
您能否发布
list和objects的样本。 -
@MartinGottweis 添加了示例。
-
@Joe 是的,它们是独一无二的——没有重复或虚假值。我想十字路口是我要找的!
标签: javascript arrays loops