I have JSON data. I need only specific key-value pairs to be passed as data to a component. I used delete
and deleted existing key-value pair except what I needed so that after deleting, only key-value pairs that I needed would exist. I got my code working perfectly but, I'm not satisfied by using delete
to make my code work. I need the JSON data filtered with the key-value pairs that I specify.
JSON data will be like,
commentProps = {
comments:"example",
author:"authorName",
ownerId:"1029989",
objectId:"152039900"
};
Code that I'm using right now,
renderItem={props => {
const commentProps = Object.assign({}, props);
delete commentProps.ownerId;
delete commentProps.objectId;
return <Comment {...commentProps}/>
}}
This returns JSON data as,
commentProps = {comments:"example",author:"authorName"};
I need this to achieve without using delete. Any help would be appreciated. Thanks in advance!
// Using rest operator ignore delete props:
renderItem = props => {
const {ownerId, objectId, ...rest} = props
return <Comment {...rest} />;
};
// Best and reccomended, you should not let go all props:
renderItem = props => {
const {comments, author} = props
return <Comment comments={comments} author={author} />;
};
With your solution
renderItem={props => {
const commentProps = Object.assign({}, {comments: props.comments, author: props.author});
return <Comment {...commentProps}/>
}}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.