DEV Community

anderu
anderu

Posted on • Edited on

1

The easy way to get unique values in array of array Javascript

const catsData = [
    {
        emotionTags: ["moody"],
        isGif: false,
        image: "angry.jpeg",
        alt: "A cat looking moody",
    },
    {
        emotionTags: ["moody", "insomniac"],
        isGif: false,
        image: "angry2.jpeg",
        alt: "A cat looking moody",
    },
    ...........]
Enter fullscreen mode Exit fullscreen mode

Today I learn an easy method to get unique values in array of array. The catsData above show there is an array of object and i want to get the object.emotionTags data.

function getEmotionsArray(cats){
    const emotionsArray = [];
    for (let cat of cats) {
       emotionsArray.push(cat.emotionTags)
    }
    let flattenArr = emotionsArray.flat()
    return [...new Set(flattenArr)]
}
Enter fullscreen mode Exit fullscreen mode

I setup getEmotionsArray(cats) function

  • I set and empty array emotionsArray.

  • I use for of method to loop thru the cats data and then push emotionTags array into emotionsArray.

[["moody"], ["moody", "insomniac"], ["moody"], ["confused", "sad"],.....]
Enter fullscreen mode Exit fullscreen mode
  • The emotionsArray will have array of array.
["moody", "moody", "insomniac", "moody", "confused", "sad",...]
Enter fullscreen mode Exit fullscreen mode
  • Then I use Array.prototype.flat() method to flatten emotionsArray.
["moody", "insomniac", "confused", "sad", "dominant", "happy", "relaxed", "hungry", "scared"]
Enter fullscreen mode Exit fullscreen mode
  • To get the unique values in the array simply use [...new Set(flattenArr)].

Eat, sleep, code, repeat!
Andrew Tan

Hostinger image

Get n8n VPS hosting 3x cheaper than a cloud solution

Get fast, easy, secure n8n VPS hosting from $4.99/mo at Hostinger. Automate any workflow using a pre-installed n8n application and no-code customization.

Start now

Top comments (2)

Collapse
 
lionelrowe profile image
lionel-rowe

Looks like a perfect use case for flatMap:

The flatMap() method of Array instances returns a new array formed by applying a given callback function to each element of the array, and then flattening the result by one level. It is identical to a map() followed by a flat() of depth 1 (arr.map(...args).flat()), but slightly more efficient than calling those two methods separately.

Then you can write it in just 1 line:

const emotions = [...new Set(catsData.flatMap((cat) => cat.emotionTags))]
Enter fullscreen mode Exit fullscreen mode
Collapse
 
anderutan profile image
anderu

Hi @lionelrowe, thanks for your comment!

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay