我有这样的结构:
var arr = [
{
title: 'anchorman'
},
{
title: 'happy gilmore'
},
{
title: 'anchorman'
}
]
现在我需要做些什么来结束这样的数组:
var arr = [
{
title: 'anchorman'
}
]
因此,它不仅删除了唯一的条目,而且只留下一个条目用于复制。
到目前为止,我有这个但是不好!
var ref;
for(var i in movies) {
ref = movies[i].title;
if(this.titles.indexOf(ref) == -1) {
movies.splice(i, 1);
} else {
this.titles.push(ref);
}
}
其中'movies'是这个问题中的第一个数组,this.titles只是一个空数组。
答案 0 :(得分:0)
以下代码将创建具有所需结果的新数组:jsfiddle
var arr = [
{
title: 'anchorman'
},
{
title: 'happy gilmore'
},
{
title: 'anchorman'
}
];
var temp = {}, newArr = [];
for(var k =0;k< arr.length; k++){
if(temp[arr[k].title]){
newArr.push(arr[k]);
}
temp[arr[k].title] = true;
}
arr = newArr;
//console.log(newArr);