let posts = [{
"8xf0y6ziyjabvozdd253nd": {
"id": "8xf0y6ziyjabvozdd253nd",
"timestamp": 1467166872634,
"title": "Udacity is the best place to learn React",
"body": "Everyone says so after all.",
"author": "thingtwo",
"category": "react",
"voteScore": 6,
"deleted": false
}
}, {
"8888ziyjabvozdd253nd": {
"id": "8888ziyjabvozdd253nd",
"timestamp": 1467166872634,
"title": "Udacity is the best place to learn React",
"body": "Everyone says so after all.",
"author": "thingtwo",
"category": "react",
"voteScore": 6,
"deleted": true
}
}, {
"77776ziyjabvozdd253nd": {
"id": "77776ziyjabvozdd253nd",
"timestamp": 1467166872634,
"title": "Udacity is the best place to learn React",
"body": "Everyone says so after all.",
"author": "thingtwo",
"category": "react",
"voteScore": 6,
"deleted": false
}
}, {
"6666ziyjabvozdd253nd": {
"id": "6666ziyjabvozdd253nd",
"timestamp": 1467166872634,
"title": "Udacity is the best place to learn React",
"body": "Everyone says so after all.",
"author": "thingtwo",
"category": "react",
"voteScore": 6,
"deleted": true
}
}, {
"55556ziyjabvozdd253nd": {
"id": "55556ziyjabvozdd253nd",
"timestamp": 1467166872634,
"title": "Udacity is the best place to learn React",
"body": "Everyone says so after all.",
"author": "thingtwo",
"category": "react",
"voteScore": 6,
"deleted": false
}
}];
const filteredPosts = _(posts)
.filter(post => {
const objectKey = Object.keys(post)[0];
const innerData = post[objectKey];
return innerData.deleted === false
})
.map(post => {
const objectKey = Object.keys(post)[0];
return `<div key={${objectKey}}>`
})
.valueOf();
console.log(filteredPosts);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>
posts.find
应该获取一个函数... 而不是元素和对象。在发布之前请再次确认。 - Dekelfind
返回单个实例或者如果没有匹配则返回undefined
。map
可用于数组上。map
不能像这样链式使用。 - Matt Byrne