How to pass a react prop if it's deeply nested inside a JSON object?










1















I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.



Here is the code



import React, Component from "react";

import "./ItemTable.css";

class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price &nbsp;
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);



export default ItemTable;


...and here's my App component...



import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";

import shop from "./data/shop.json";

import "./App.css";

class App extends Component
state =
data: shop
;

priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;

render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);



export default App;


My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...



[

"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":



The question is: how do i pass the price from my JSON file?










share|improve this question
























  • what does console.log(key) gives you in priceSort ?

    – juvian
    Nov 13 '18 at 15:35











  • Hi! It gives me data

    – doglabel
    Nov 13 '18 at 15:45











  • try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

    – juvian
    Nov 13 '18 at 15:47











  • Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

    – doglabel
    Nov 13 '18 at 15:48











  • You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

    – Tholle
    Nov 13 '18 at 15:49
















1















I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.



Here is the code



import React, Component from "react";

import "./ItemTable.css";

class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price &nbsp;
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);



export default ItemTable;


...and here's my App component...



import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";

import shop from "./data/shop.json";

import "./App.css";

class App extends Component
state =
data: shop
;

priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;

render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);



export default App;


My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...



[

"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":



The question is: how do i pass the price from my JSON file?










share|improve this question
























  • what does console.log(key) gives you in priceSort ?

    – juvian
    Nov 13 '18 at 15:35











  • Hi! It gives me data

    – doglabel
    Nov 13 '18 at 15:45











  • try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

    – juvian
    Nov 13 '18 at 15:47











  • Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

    – doglabel
    Nov 13 '18 at 15:48











  • You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

    – Tholle
    Nov 13 '18 at 15:49














1












1








1








I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.



Here is the code



import React, Component from "react";

import "./ItemTable.css";

class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price &nbsp;
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);



export default ItemTable;


...and here's my App component...



import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";

import shop from "./data/shop.json";

import "./App.css";

class App extends Component
state =
data: shop
;

priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;

render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);



export default App;


My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...



[

"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":



The question is: how do i pass the price from my JSON file?










share|improve this question
















I'm making a table of products for a shop (the headers are: id, title, imagePath, newPrice, oldPrice) coming from a JSON file and got an ItemTable component created in my React app to iterate over the contents of it.



Here is the code



import React, Component from "react";

import "./ItemTable.css";

class ItemTable extends Component
render()
return (
<table>
<thead>
<tr>
<th>#</th>
<th>Title</th>
<th>Image</th>
<th>
Price &nbsp;
<button onClick=() => this.props.priceSort("title")>
Sort
</button>
</th>
<th>Old price</th>
</tr>
</thead>
<tbody>
this.props.data.map(row => (
<tr key=row.id>
<td>row.id</td>
<td>row.data.title</td>
<td>
<img src=row.data.base_url alt="" />
</td>
<td>row.data.price</td>
<td>row.data.oldPrice</td>
</tr>
))
</tbody>
</table>
);



export default ItemTable;


...and here's my App component...



import React, Component from "react";
import ItemTable from "./components/ItemTable/ItemTable";

import shop from "./data/shop.json";

import "./App.css";

class App extends Component
state =
data: shop
;

priceSort = key =>
this.setState(
data: shop.sort((a, b) => parseFloat(b[key]) - parseFloat(a[key]))
);
;

render()
return (
<div className="page-container">
<ItemTable data=this.state.data priceSort=this.priceSort />
</div>
);



export default App;


My priceSort() function dosn't work though because I can't figure out the way to pass a deeply nested JSON prop. Here's a part of my JSON for you to get the picture...



[

"id": 2,
"data":
"price": 990,
"cross_category_id": "733",
"id": "95361595",
"available": "true",
"base_url": "http://image01.bonprix.ru/assets/319x448/1530596198/18164321-KFBpLZ9R.jpg",
"target_url": "https://upload.wikimedia.org/wikipedia/commons/thumb/8/8e/White_stiletto_high-heels.jpg/300px-White_stiletto_high-heels.jpg",
"title": "High heeled shoes",
"url": "http://ssl.hurra.com/TrackIt?tid=10087150C577PPC&url=[[http://www.bonprix.ru/produkty/pulover-s-vysokim-vorotnikom-chernyj-953615/?landmark=Entry&wkz=98&iwl=291&typ=POR&anbieter=Soloway&aktion=POR_VKB&version=SSP_NAME&promo=promo]]",
"discount": 0,
"categoryId": ["733"],
"description_id": "default",
"star": "4",
"picture": [
"sizetype": "st_w240_h310", "path": "st_w240_h310/2/335/95361595" ,
"path": "st_w300_h500/2/335/95361595", "sizetype": "st_w300_h500" ,
"sizetype": "st_w64_h90", "path": "st_w64_h90/2/335/95361595"
],
"oldPrice": 990
,
"main": 1,
"refs":



The question is: how do i pass the price from my JSON file?







javascript reactjs






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 15:26









Federico klez Culloca

16.1k134380




16.1k134380










asked Nov 13 '18 at 15:25









doglabeldoglabel

206




206












  • what does console.log(key) gives you in priceSort ?

    – juvian
    Nov 13 '18 at 15:35











  • Hi! It gives me data

    – doglabel
    Nov 13 '18 at 15:45











  • try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

    – juvian
    Nov 13 '18 at 15:47











  • Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

    – doglabel
    Nov 13 '18 at 15:48











  • You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

    – Tholle
    Nov 13 '18 at 15:49


















  • what does console.log(key) gives you in priceSort ?

    – juvian
    Nov 13 '18 at 15:35











  • Hi! It gives me data

    – doglabel
    Nov 13 '18 at 15:45











  • try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

    – juvian
    Nov 13 '18 at 15:47











  • Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

    – doglabel
    Nov 13 '18 at 15:48











  • You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

    – Tholle
    Nov 13 '18 at 15:49

















what does console.log(key) gives you in priceSort ?

– juvian
Nov 13 '18 at 15:35





what does console.log(key) gives you in priceSort ?

– juvian
Nov 13 '18 at 15:35













Hi! It gives me data

– doglabel
Nov 13 '18 at 15:45





Hi! It gives me data

– doglabel
Nov 13 '18 at 15:45













try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

– juvian
Nov 13 '18 at 15:47





try shop.sort((a, b) => parseFloat(b.data.price) - parseFloat(a.data.price)

– juvian
Nov 13 '18 at 15:47













Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

– doglabel
Nov 13 '18 at 15:48





Sorry for confusing, I just need the function to sort by price, so it should be <button onClick=() => this.props.priceSort("price")> in the ItemTable component Now it console.logs "price"

– doglabel
Nov 13 '18 at 15:48













You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

– Tholle
Nov 13 '18 at 15:49






You could use something like dlv and pass in data.price as key. shop.sort((a, b) => parseFloat(dlv(b, key, 0)) - parseFloat(dlv(a, key, 0)))

– Tholle
Nov 13 '18 at 15:49













1 Answer
1






active

oldest

votes


















1














Because in your priceSort you're no getting any data to sort.
Try this:



priceSort = key => 
this.setState(( data: shop ) =>
const sortedData = shop.sort((a, b) =>
parseFloat(b['data'][key]) - parseFloat(a['data'][key])
);
return data: sortedData
);
;





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284243%2fhow-to-pass-a-react-prop-if-its-deeply-nested-inside-a-json-object%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Because in your priceSort you're no getting any data to sort.
    Try this:



    priceSort = key => 
    this.setState(( data: shop ) =>
    const sortedData = shop.sort((a, b) =>
    parseFloat(b['data'][key]) - parseFloat(a['data'][key])
    );
    return data: sortedData
    );
    ;





    share|improve this answer





























      1














      Because in your priceSort you're no getting any data to sort.
      Try this:



      priceSort = key => 
      this.setState(( data: shop ) =>
      const sortedData = shop.sort((a, b) =>
      parseFloat(b['data'][key]) - parseFloat(a['data'][key])
      );
      return data: sortedData
      );
      ;





      share|improve this answer



























        1












        1








        1







        Because in your priceSort you're no getting any data to sort.
        Try this:



        priceSort = key => 
        this.setState(( data: shop ) =>
        const sortedData = shop.sort((a, b) =>
        parseFloat(b['data'][key]) - parseFloat(a['data'][key])
        );
        return data: sortedData
        );
        ;





        share|improve this answer















        Because in your priceSort you're no getting any data to sort.
        Try this:



        priceSort = key => 
        this.setState(( data: shop ) =>
        const sortedData = shop.sort((a, b) =>
        parseFloat(b['data'][key]) - parseFloat(a['data'][key])
        );
        return data: sortedData
        );
        ;






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 13 '18 at 15:56









        Tholle

        43.1k54770




        43.1k54770










        answered Nov 13 '18 at 15:52









        Luis GarcíaLuis García

        362




        362





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53284243%2fhow-to-pass-a-react-prop-if-its-deeply-nested-inside-a-json-object%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

            Edmonton

            Crossroads (UK TV series)