admin管理员组

文章数量:1323529

var data = [
    {author: 'foo', ment: 'nice'},
    {author: 'bar', ment: 'wow'}
];

var CommentBox = React.createClass({
    render: function () {
        var CommentNodes = this.props.data.map(function (ment) {
            return (
                <Comment author={ment.author} ment={mentment}>
                </Comment>
            );
        });
        return (
            <div className="ment-box">
                {CommentNodes}
            </div>
        );
    }
});

var Comment = React.createClass({
    render: function () {
        return (
            <div className="ment-box ment">
                <h2 className="ment-author">
                    {this.props.author}
                </h2>
                {this.propsment}
            </div>
        );
    }
});

React.render(<CommentBox data={data}/>, document.getElementById("example"));

In the piece of code, I just pass the parameter to Comment using data. Since data is a object, which similar to Python's dict. So I'm wondering, can I just pass data as the unpacking object? Like using ** is Python:

>>> def show(**kwargs):
...     return kwargs
... 
>>> items = {'a': 1, 'b': 2}
>>> print(show(**items))
{'a': 1, 'b': 2}
var data = [
    {author: 'foo', ment: 'nice'},
    {author: 'bar', ment: 'wow'}
];

var CommentBox = React.createClass({
    render: function () {
        var CommentNodes = this.props.data.map(function (ment) {
            return (
                <Comment author={ment.author} ment={ment.ment}>
                </Comment>
            );
        });
        return (
            <div className="ment-box">
                {CommentNodes}
            </div>
        );
    }
});

var Comment = React.createClass({
    render: function () {
        return (
            <div className="ment-box ment">
                <h2 className="ment-author">
                    {this.props.author}
                </h2>
                {this.props.ment}
            </div>
        );
    }
});

React.render(<CommentBox data={data}/>, document.getElementById("example"));

In the piece of code, I just pass the parameter to Comment using data. Since data is a object, which similar to Python's dict. So I'm wondering, can I just pass data as the unpacking object? Like using ** is Python:

>>> def show(**kwargs):
...     return kwargs
... 
>>> items = {'a': 1, 'b': 2}
>>> print(show(**items))
{'a': 1, 'b': 2}
Share Improve this question edited Jan 31, 2016 at 3:26 Dmitry Shvedov 3,2964 gold badges40 silver badges56 bronze badges asked Sep 15, 2015 at 18:35 user2394303user2394303 1
  • Looks like data is an array and this.props.data.map is unpacking the array. – J. Mark Stevens Commented Sep 15, 2015 at 21:27
Add a ment  | 

2 Answers 2

Reset to default 6

As @AlexPalcuie answers above you can use object spread to acplish exactly what python ** operators do.

So this is equivalent to your code above:

var CommentBox = React.createClass({
    render: function () {
        var CommentNodes = this.props.data.map(function (ment) {
            return (
                <Comment {...ment}>
                </Comment>
            );
        });
        return (
            <div className="ment-box">
                {CommentNodes}
            </div>
        );
    }
});

You can use the spread attributes syntax.

本文标签: pythonCan I pass unpacking object as parameter in Javascript or JSXStack Overflow