简体   繁体   English

在 React/js 中从数组中查找唯一值

[英]Find unique values from an array in React/js

I'm a beginner in JavaScript and an even bigger beginner in ReactJS.我是 JavaScript 的初学者,也是 ReactJS 的初学者。 Most of the following I've taken from tutorials and am trying to adapt it.以下大部分内容都是我从教程中获取的,并正在尝试对其进行调整。 So the codes displays all the values from the "tag" (people, places, places, etc.).因此,代码显示“标签”中的所有值(人物、地点、地点等)。 It displays it as inline li elements as it's going to be a menu.它将它显示为内联 li 元素,因为它将成为一个菜单。 The problem is that it displays all the tag for all the elements.问题是它显示了所有元素的所有标签。 I just want it to catch the unique 'tag' values for the menu (eg. people, places, things,...).我只是想让它捕捉菜单的独特“标签”值(例如人物、地点、事物……)。 I've tried _.uniq but it did not display the array.我试过 _.uniq 但它没有显示数组。 Thank you.谢谢你。

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>
    );
  }
}

GalleryHeader画廊标题

import React from "react";


export default class GalleryHeader extends React.Component {

  render() {

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

Images are stored in:图像存储在:

Gallery画廊

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>
    );
  }
}

As I understand, you want to filter out original image collection by given tag.据我了解,您想按给定的标签过滤掉原始图像集合。 Correct?正确的?
If so, create filter function, which accepts tag and collection to filter.如果是这样,创建过滤器函数,它接受标签和集合进行过滤。 You can reuse this function then.然后你可以重用这个函数。

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);

Edit编辑
Added code to pick all unique tag names.添加了选择所有唯一标签名称的代码。 Dependency free.无依赖。 Unline Set, this works in IE < 11. Unline Set,这适用于 IE < 11。

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

It is similar The SQL SELECT DISTINCT Statement in javascript它类似于 javascript 中的 SQL SELECT DISTINCT 语句

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

Since they are plain strings you can create a JavaScript Set for that.由于它们是纯字符串,因此您可以为此创建一个 JavaScript Set

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

As stated in this documentation[1] we can use the uniqBy method to return the unique element in an object array.如本文档[1] 所述,我们可以使用uniqBy方法返回对象数组中的唯一元素。 Please refer the below code.请参考以下代码。

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

This will return an output like this [{ 'id': 100 }, { 'id': 200 }, { 'id': 300 }]这将返回这样的输出[{ 'id': 100 }, { 'id': 200 }, { 'id': 300 }]

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

Typescript sets source打字稿设置

For those who are using Typescripts I would recomend TypeScript Set对于那些使用 Typescripts 的人,我会推荐TypeScript Set

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM