admin管理员组文章数量:1341465
I have a JSONArray returned by the server. I want to use .map() on it so that i can get key,values pairs of every object present in that array. I have written following code but i am getting error "files.map is not a function". Can anyone please help me resolve this?
showUploadedFiles()
{
const page = 1;
const items_per_page = this.state.event.file_ids.length;
getAllTaskFiles(this.state.event.id, page, items_per_page).then((allFiles) => {
this.renderUploadedFiles(allFiles);
});
}
renderUploadedFiles(files)
{
let details = null;
details = files.map((singleFile) => {
return (
<div>
<a href="#" >{singleFile.filename}</a>
<a href="#" >{singleFile.file_path}</a>
</div>
);
});
}
My JSONArray is:
[{"file_id": 5224879255191552, "filename": "children_walk_friends_forest_116878_3840x2160.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC81MjI0ODc5MjU1MTkxNTUyL2Zha2UtemRNeklhZ1RTYl9PN3RXY2pXbkhVQT09"}, {"file_id": 4943404278480896, "filename": "banner_old.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC80OTQzNDA0Mjc4NDgwODk2L2Zha2UtcmJjbDBVdVFzVmZkMjRtRUV2ME1xZz09"}]
I have a JSONArray returned by the server. I want to use .map() on it so that i can get key,values pairs of every object present in that array. I have written following code but i am getting error "files.map is not a function". Can anyone please help me resolve this?
showUploadedFiles()
{
const page = 1;
const items_per_page = this.state.event.file_ids.length;
getAllTaskFiles(this.state.event.id, page, items_per_page).then((allFiles) => {
this.renderUploadedFiles(allFiles);
});
}
renderUploadedFiles(files)
{
let details = null;
details = files.map((singleFile) => {
return (
<div>
<a href="#" >{singleFile.filename}</a>
<a href="#" >{singleFile.file_path}</a>
</div>
);
});
}
My JSONArray is:
[{"file_id": 5224879255191552, "filename": "children_walk_friends_forest_116878_3840x2160.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC81MjI0ODc5MjU1MTkxNTUyL2Zha2UtemRNeklhZ1RTYl9PN3RXY2pXbkhVQT09"}, {"file_id": 4943404278480896, "filename": "banner_old.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC80OTQzNDA0Mjc4NDgwODk2L2Zha2UtcmJjbDBVdVFzVmZkMjRtRUV2ME1xZz09"}]
Share
edited Aug 27, 2017 at 11:25
Muhammad Hamid Raza
asked Aug 27, 2017 at 11:08
Muhammad Hamid RazaMuhammad Hamid Raza
2052 gold badges4 silver badges15 bronze badges
6
-
5
Are you sure that
allFiles
orfiles
are really arrays? Have you tried aconsole.log()
before.map()
? – Nocebo Commented Aug 27, 2017 at 11:11 - @Nocebo Yes, the JSONArray which i posted is actually what console.log gave me. – Muhammad Hamid Raza Commented Aug 27, 2017 at 11:17
-
2
Now try
console.log( typeof files )
and see what it says – adeneo Commented Aug 27, 2017 at 11:17 - 2 I think i should first parse it. am i right? – Muhammad Hamid Raza Commented Aug 27, 2017 at 11:20
- 2 yes use JSON.parse(files).map() – Dinesh undefined Commented Aug 27, 2017 at 11:20
1 Answer
Reset to default 7As pointed out by other users, you have to use JSON.parse()
to get object from your string. Here is the code snippet, storing your files in an array.
const str = '[{"file_id": 5224879255191552, "filename": "children_walk_friends_forest_116878_3840x2160.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC81MjI0ODc5MjU1MTkxNTUyL2Zha2UtemRNeklhZ1RTYl9PN3RXY2pXbkhVQT09"}, {"file_id": 4943404278480896, "filename": "banner_old.jpg", "file_path": "/api/files/encoded_gs_file%3AdHJhY2tpbmctYXBpL3YxL2FjY291bnRzLzU2Mjk0OTk1MzQyMTMxMjAvdGFza3MvNDk5NjE4MDgzNjYxNDE0NC80OTQzNDA0Mjc4NDgwODk2L2Zha2UtcmJjbDBVdVFzVmZkMjRtRUV2ME1xZz09"}]';
const files = JSON.parse(str);
const details = files.map((singleFile) => {
return (
`<div>
<a href="#">{singleFile.filename}</a>
<a href="#">{singleFile.file_path}</a>
</div>`
);
});
console.log(details);
.as-console-wrapper { max-height: 100% !important; top: 0; }
本文标签: javascriptHow to use map() function on JSONArray in reactjs ES6Stack Overflow
版权声明:本文标题:javascript - How to use .map() function on JSONArray in reactjs ES6 - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743668671a2519165.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论