Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.8k views
in Technique[技术] by (71.8m points)

javascript - Remove duplicates in array list react

array name stays and it duplicates and repeating this process just clogs the list up.

Thank you.

        setListItems(contents.data);
        console.log(contents.data);


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To convert the array contents.data to Set, do this:

const setData = new Set(contents.data);

That will remove all the duplicate items. Then to convert it back, do this:

const uniqueArray = Array.from(setData);

The above will only work if the original array (contents.data) consisted of primitive values. If it was an array of objects then this will not work as-is and will require some changes.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...