【问题标题】:Find unique values from an array in React/js在 React/js 中从数组中查找唯一值
【发布时间】:2016-03-13 21:36:11
【问题描述】:

我是 JavaScript 的初学者,也是 ReactJS 的更大的初学者。我从教程中获取了以下大部分内容,并正在尝试对其进行调整。因此代码显示来自“标签”的所有值(人物、地点、地点等)。它将它显示为内联 li 元素,因为它将成为一个菜单。问题是它显示了所有元素的所有标签。我只想让它捕捉菜单的独特“标签”值(例如人、地点、事物……)。我试过 _.uniq 但它没有显示数组。谢谢。

import React from "react";
import GalleryHeader from './GalleryHeader';
import ImageEntry from './ImageEntry';
import _ from 'lodash';

export default class GalleryImages extends React.Component {

    renderItems() {
        return _.map(this.props.images, (image, index) => <ImageEntry key={index} {...image} />);
    }

  renderMenu() {
    return _.map(this.props.images, (image, index) => <GalleryHeader key={index} {...image} />);
  }


  render() {

    return (
      <div>
      <ul class="list-inline">
        {this.renderMenu()}
      </ul>
        {this.renderItems()}          
      </div>
    );
  }
}

画廊标题

import React from "react";


export default class GalleryHeader extends React.Component {

  render() {

    return (
            <li>{this.props.tag}</li>
    );
  }
}

图像存储在:

图库

import React from "react";

import GalleryImages from './Gallery/GalleryImages';


var images = [{
  "id": 1,
  "tag": "people",
  "src": "img/img1.jpg",
  "bsrc": "img/img1b.jpg",
  "alt": "Some description"
}, {
  "id": 2,
  "tag": "places",
  "src": "img/img2.jpg",
  "bsrc": "img/img2b.jpg",
  "alt": "Some description"
}, {
  "id": 3,
  "tag": "places",
  "src": "img/img3.jpg",
  "bsrc": "img/img3b.jpg",
  "alt": "Some place description"
}, {
  "id": 4,
  "tag": "things",
  "src": "img/img4.jpg",
  "bsrc": "img/img4b.jpg",
  "alt": "Internet of things"
}, {
  "id": 5,
  "tag": "people",
  "src": "img/img5.jpg",
  "bsrc": "img/img5b.jpg",
  "alt": "A personal person"
}, {
  "id": 6,
  "tag": "places",
  "src": "img/img6.jpg",
  "bsrc": "img/img6b.jpg",
  "alt": "Interesting place"
}, {
  "id": 7,
  "tag": "people",
  "src": "img/img7.jpg",
  "bsrc": "img/img7b.jpg",
  "alt": "Tarabaora"
}, {
  "id": 8,
  "tag": "places",
  "src": "img/img6.jpg",
  "bsrc": "img/img6b.jpg",
  "alt": "Interesting place"
}, {
  "id": 9,
  "tag": "places",
  "src": "img/img6.jpg",
  "bsrc": "img/img6b.jpg",
  "alt": "Interesting place"
}];

export default class Gallery extends React.Component {
  constructor(props) {
    super(props);

    this.state = {
      images,
      people: _.filter(images, {tag: "things"}),    // or in ES6 just people
      sources: _.filter(images, {tag: "src"}),
      places: _.filter(images, {tag: "places"}),
      cats: _.uniq(images)
    };
  }

  render() {
    // This is how you can filter them
    var ima = _.filter(images, {tag: "things"});
    console.log(ima);


    return (
      <div className="koko">
        <h1>This isGalleryfemCount</h1>
        <GalleryImages images={this.state.images} />
      </div>
    );
  }
}

【问题讨论】:

    标签: javascript reactjs uniq


    【解决方案1】:

    据我了解,您想按给定标签过滤掉原始图像集。对吗?
    如果是这样,请创建过滤器函数,该函数接受标签和集合进行过滤。然后您可以重用此功能。

    var images = [{
      "id": 1,
      "tag": "people",
      "src": "img/img1.jpg",
      "bsrc": "img/img1b.jpg",
      "alt": "Some description"
    }, {
      "id": 2,
      "tag": "places",
      "src": "img/img2.jpg",
      "bsrc": "img/img2b.jpg",
      "alt": "Some description"
    }, {
      "id": 3,
      "tag": "places",
      "src": "img/img3.jpg",
      "bsrc": "img/img3b.jpg",
      "alt": "Some place description"
    }, {
      "id": 4,
      "tag": "things",
      "src": "img/img4.jpg",
      "bsrc": "img/img4b.jpg",
      "alt": "Internet of things"
    }, {
      "id": 5,
      "tag": "people",
      "src": "img/img5.jpg",
      "bsrc": "img/img5b.jpg",
      "alt": "A personal person"
    }, {
      "id": 6,
      "tag": "places",
      "src": "img/img6.jpg",
      "bsrc": "img/img6b.jpg",
      "alt": "Interesting place"
    }, {
      "id": 7,
      "tag": "people",
      "src": "img/img7.jpg",
      "bsrc": "img/img7b.jpg",
      "alt": "Tarabaora"
    }, {
      "id": 8,
      "tag": "places",
      "src": "img/img6.jpg",
      "bsrc": "img/img6b.jpg",
      "alt": "Interesting place"
    }, {
      "id": 9,
      "tag": "places",
      "src": "img/img6.jpg",
      "bsrc": "img/img6b.jpg",
      "alt": "Interesting place"
    }];
    
    const filter = (tag, arr) => arr.filter(img => img.tag === tag);
    
    // or in ES5
    // var filter = function(tag, arr) {
    //    return arr.filter(function(img) {
    //        return img.tag === tag;
    //    })
    // };
    
    const filtered = filter('people', images);
    

    编辑
    添加了代码以选择所有唯一的标签名称。无依赖。 Unline Set,这在 IE

    const uniqueTags = [];
    images.map(img => {
        if (uniqueTags.indexOf(img.tag) === -1) {
            uniqueTags.push(img.tag)
        }
    });
    

    【讨论】:

    • 谢谢。你抢先了我的下一个问题。这正是我想要做的,你的答案会派上用场。此刻,我被困在那之前的一步。试图生成一个应该是:地点、人物、事物的菜单。我上面的代码生成:人、地、地、物、人、地、人、地等)
    • 编辑了我的回复。添加了提取唯一标签名称的代码。
    • 你好安德烈。我想知道您是否可以就我的代码再回答一个问题。在 renderItems 函数中,我是否需要 (images, index) 中的 'index' 参数和随后的“key={index}”?索引是什么?它似乎没有工作。谢谢
    • 从技术上讲,您不需要它。另一方面,React 要求您在渲染数组时提供 key 属性以进行内部优化。实际上,最好避免将索引传递为key,而应传递唯一值。在您的情况下,您可以使用key={image.id},因为它是独一无二的。在您无法分配唯一值的情况下,请随意使用索引(当没有更好的选择时)
    • _.map(this.props.images, (image) =&gt; &lt;ImageEntry key={image.id} {...image} /&gt;)
    【解决方案2】:

    类似javascript中的SQL SELECT DISTINCT Statement

    const uniqueTags = [];
    images.map((item) => {
      var findItem = uniqueTags.find((x) => x.tag === item.tag);
      if (!findItem) uniqueTags.push(item);
    });
    

    【讨论】:

      【解决方案3】:

      打字稿集 source

      对于那些使用 Typescripts 我会推荐 TypeScript Set

      Array.from(new Set(yourArray.map((item: any) => item.id)))
      

      【讨论】:

        【解决方案4】:

        由于它们是纯字符串,您可以为此创建 JavaScript Set

        https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set

        【讨论】:

        • 不起作用,因为集合(数组)中的每个对象都是不同的对象(引用不同)。
        • 但是他只想要每个元素的'tag'值来构建一个菜单。他可以为每个标签字符串创建一个集合。没有?
        • 那么是的。不幸的是,您需要展平数组(在示例中给出),然后从这样的数组 const tags = new Set(images.map(img =&gt; img.tag)); 创建 Set 不幸的是,我们需要支持旧的 IE。如果没有这个,我肯定会按照你的建议使用 Set :)
        【解决方案5】:

        如本文档中所述[1],我们可以使用 uniqBy 方法返回对象数组中的唯一元素。请参考以下代码。

        import _ from 'lodash';
        _.uniqBy([{ 'id': 100 }, { 'id': 200 }, { 'id': 300 },{ 'id': 100 }], 'id');
        

        这将返回如下输出 [{ 'id': 100 }, { 'id': 200 }, { 'id': 300 }]

        [1]https://lodash.com/docs/#uniqBy

        【讨论】:

          猜你喜欢
          • 2014-02-09
          • 2017-05-07
          • 1970-01-01
          • 2015-09-20
          • 1970-01-01
          • 1970-01-01
          • 2022-11-15
          • 1970-01-01
          • 2015-12-08
          相关资源
          最近更新 更多