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;) –