Was*_*and 3 javascript uniq reactjs
我是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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
GalleryHeader
import React from "react";
export default class GalleryHeader extends React.Component {
render() {
return (
<li>{this.props.tag}</li>
);
}
}
Run Code Online (Sandbox Code Playgroud)
图像存储在:
画廊
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>
);
}
}
Run Code Online (Sandbox Code Playgroud)
据我了解,您希望通过给定标签过滤掉原始图像集合.正确?
如果是,请创建过滤器函数,该函数接受标记和集合以进行过滤.然后,您可以重用此功能.
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);
Run Code Online (Sandbox Code Playgroud)
编辑
添加的代码以选择所有唯一标记名称.依赖免费.Unline Set,这适用于IE <11.
const uniqueTags = [];
images.map(img => {
if (uniqueTags.indexOf(img.tag) === -1) {
uniqueTags.push(img.tag)
}
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9749 次 |
| 最近记录: |