본문 바로가기

카테고리 없음

Filter 로 중복제거하기

 
 
"blogs": [
{
"id": 1,
"title": "My First Blog",
"body": "Why do we use it?\nIt is a long established fact that a reader will be distracted by the readable content of a 
"author": "김코딩",
"likes": 30
},
{
"id": 2,
"title": "Opening Party!",
"body":  "Ipsum is therefore always free from repetition, injected humour, or non-characteristic words etc.",
"author": "박해커",
"likes": 100
},
{
"id": 3,
"title": "testing",
"body": "cds,fknsdf dgfsdflsd",
"author": "김코딩",
"likes": 10
},

//[김코딩,박해커,김코딩]

const authrUnique = blogs &&(blogs.filter((character, idx, arr) =>{

    return arr.findIndex(item => item.author === character.author) === idx)

}));

 

 

....ex

const arrDup = ['라이언', '어피치', '프로도', '콘', '라이언', '프로도'];

const arrUnique = arrDup.filter((val, idx) => {

       return arrDup.indexOf(val) === idx; //값이 처음나오는 배열 인덱스와 현재 인덱스가 같으면 포함 });

 

console.log(arrUnique); // ['라이언', '어피치', '프로도', '콘']