0
我有一份每日发布的报纸文章列表。由于许多报纸都是大型连锁店的一部分,我不想看到同一篇文章的每一个版本,但是我们希望看到它有多少其他网点。删除数组中的重复项,但注释其余的行有其他人
So..this是想我想看看
条 来源 - 国家邮政局,另外在西雅图大火,纽约时报
第2条 来源 - 华盛顿邮报
我这样做成功使用此代码..但它似乎笨重
示例JSON
var data = {
"articles": [
{
"id": "1",
"title": "xxxx'",
"body": "<p>Body goes here",
"publication": {
"id": 1,
"name": "National Post"
},
"articleUrl": "http://www.foo.com/1"
},
{
"id": "2",
"title": "yyyy'",
"body": "<p>Body goes here",
"publication": {
"id": 1,
"name": "Washington Post"
},
"articleUrl": "http://www.foo.com/2"
},
{
"id": "3",
"title": "xxxx'",
"body": "<p>Body goes here",
"publication": {
"id": 1,
"name": "Seattle Blaze"
},
"articleUrl": "http://www.foo.com/3"
},
{
"id": "4",
"title": "xxxx'",
"body": "<p>Body goes here",
"publication": {
"id": 1,
"name": "New York Times"
},
"articleUrl": "http://www.foo.com/4"
}
]
}
js.utils.RemoveDups = function RemoveDups(json) {
var articles = new Array();
for (var i = 0; i < json.length; i++) {
var seen = false;
for (var j = 0; j != articles.length; ++j) {
if (json[i] != null && articles[j] != null) {
if (articles[j].title == json[i].title) {
seen = true;
articles[j].publication.name = articles[j].publication.name + ", <a href='" + json[i].articleUrl + "' target='_blank'>" + json[i].publication.name + '</a>';
}
}
}
if (!seen) articles.push(json[i]);
}
return articles;
};
现在我这个代码,这是更紧凑,更搞乱可能更快,但因为我不从
dataArr = data.map(function (item) { return item.title });
有完整的对象,我不能返回当前出版物的名称我是除去
//Clean the Data
if (json != null) {
var data = json.articles,
dataArr = data.map(function (item) { return item.title });
//Remove Duplicates
dataArr.some(function (item, index) {
var isDuplicate = dataArr.indexOf(item, index + 1) !== -1;
if (isDuplicate) {
data[index].publication.name = data[index].publication.name + ',' + item[index].publication.name //<- dont have full object
data = removeDuplicate(data, item);
}
});
function removeDuplicate(data, title) {
$.each(data, function (index) {
if (this.title == title) {
data.splice(index, 1);
return false;
}
});
return data;
}
:奖金的问题......我不能完全肯定该机采用哪些参数来确定哪个副本,以保持和删除...理想情况下,我会想保留的版本中,项目对象(item.wordCount)的wordCount是高的st ...
对不起,也许我只是一个深夜,但究竟我在此代码替换?我通过它,但它只是返回标题,而不是对象? –
我已经更新了答案以显示整个功能。我想不出为什么它只会返回标题,而不是整篇文章。 – Barmar
我担心我在原来的问题上误导了你。请看看更新后的问题是否更有意义,因为这行不会被触及(文章[title] .publication.name + =','+ a.publication.name;) –