0

I am attempting to build an array of unique values pulled from an array of objects. Here is the data structure:

            data: [
           {
                "tags": [
                    {
                        "name": "tag",
                        "value": "Bold"
                    },
                    {
                        "name": "tag",
                        "value": "Crazy"
                    },
                    {
                        "name": "tag",
                        "value": "Colorful"
                    },
                    {
                        "name": "tag",
                        "value": "Smooth"
                    }
                ],
                "rating": 5,
                "userNickName": "lookinGood",
                "userLocation": "USA",
                "title": "LOVE IT",
                "reviewText": "Blah blah blah",
                "submissionTime": "14 January 2013"
            },
            {
                "tags": [
                    {
                        "name": "tag",
                        "value": "Colorful"
                    },
                    {
                        "name": "tag",
                        "value": "Too bright"
                    }
                ],
                "rating": 4,
                "userNickName": "CrazyColors",
                "userLocation": "USA",
                "title": "Kept It",
                "reviewText": "Hoopity hoo blah",
                "submissionTime": "13 December 2014"
            }
]

Here is what my code looks like so far:

    let tags: Array<string> = _.sortBy(
            _.uniq(
            this.props.reviewData.reviews.map(
                (o) => {
                    return o.tags && o.tags.value ? o.tags.value.trim() : '';
                }
            )
        )
    );

But tags keeps coming through blank. I've also tried return _.map(v.tags, 'value') but that returns multiple arrays. So how can I grab one of each tag value from multiple objects?

1 Answer 1

1

You need to extract the tag arrays, flatten the arrays into one array, and get the unique values:

var result = _(data) // start the chain
  .map('tags')  // extract the tag arrays
  .flatten() // combine to one array
  .uniqWith(function(tag1, tag2) {
    return _.get(tag1, 'value', '').trim() === _.get(tag2, 'value', '').trim();
  }) // get the unique values
  .sortBy('value');

var data = [{
  "tags": [{
    "name": "tag",
    "value": "Bold"
  }, {
    "name": "tag",
    "value": "Crazy"
  }, {
    "name": "tag",
    "value": "Colorful"
  }, {
    "name": "tag",
    "value": "Smooth"
  }],
  "rating": 5,
  "userNickName": "lookinGood",
  "userLocation": "USA",
  "title": "LOVE IT",
  "reviewText": "Blah blah blah",
  "submissionTime": "14 January 2013"
}, {
  "tags": [{
    "name": "tag",
    "value": "Colorful"
  }, {
    "name": "tag",
    "value": "Too bright"
  }],
  "rating": 4,
  "userNickName": "CrazyColors",
  "userLocation": "USA",
  "title": "Kept It",
  "reviewText": "Hoopity hoo blah",
  "submissionTime": "13 December 2014"
}];

var result = _(data) // start the chain
  .map('tags')  // extract the tag arrays
  .flatten() // combine to one array
  .uniqWith(function(tag1, tag2) {
    return _.get(tag1, 'value', '').trim() === _.get(tag2, 'value', '').trim();
  }) // get the unique values
  .sortBy('value');

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.2/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.