JSX / Lodash - 返回数组

时间:2016-11-30 21:24:34

标签: arrays react-native lodash react-jsx jsx

我正在尝试构建从对象数组中提取的唯一值数组。这是数据结构:

            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"
            }
]

以下是我的代码到目前为止的样子:

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

但标签一直空白。我也试过了return _.map(v.tags, 'value')但是返回了多个数组。那么如何从多个对象中获取每个标记值中的一个呢?

1 个答案:

答案 0 :(得分:1)

您需要提取标记数组,将数组展平为一个数组,并获取唯一值:

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>