Skip to content

How to Remove jsobj elements with same key-value in list(array) #9

@euije

Description

@euije

Discription

  • 검색 기능을 위해, list state로 관리 되고 있는 galleries로부터 검색어를 포함 하고 있는 element를 searchedGalleries에 저장
  • element는 jsobj 타입이며, 검색어와 value of "title", "tags"와 비교
  • 검색어는 state로 관리되고, 상위 컴포넌트로 부터 props로 넘겨 받음.
  • filter()을 활용하여 "tags의 요소들과, title" 그리고 props와 비교하여 searchedGalleries에 저장
{
    album_id: 3,
    title: "2019 상해 여행 ✈️",
    tags: [
        "여행",
        "김진우",
        "상하이"
    ]
},
{
    album_id: 4,
    title: "벚꽃 놀이 🌸",
    tags: [
        "여행"
    ]
}

setSearchedGalleries((e) => e =
galleries.filter((gallery) => gallery.title.includes(props.input[0]))
.concat(galleries.filter((gallery) => isSearchTagInList(gallery, props.input[0])))
);


Problem

  • 위의 코드를 통하여 "여행"를 props로 내려주면, searchedGalleries에는 3개의 요소가 저장됨.
  • title을 비교하는 구문을 통해 1개가, tags를 비교하는 문구를 통해 2개가 저장.
  • album_id는 고유한 값이라 중복되는 요소를 제거하는 코드를 작성하여도 적용이 안됨.
var result = arr.filter((item1, idx1)=>{
    return arr.findIndex((item2, idx2)=>{
        return item1.id == item2.id;
    }) == idx1;
});
console.table(result);

https://ratseno.tistory.com/98

  • "Json array 중복 제거"라는 키워드로 구글링 해서 위와 같은 여러 코드를 참조해서 작성해보았지만 잘 안되네요 😭

Screenshot

ezgif com-gif-maker

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions