Recursively check object for first array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
0
down vote

favorite












I am recursively checking an object to:



  • Find the largest JSON object

  • Find the array within it

It has to be rather flexible, as it should work with any structure of JSON data.



The below works, but it isn't flexible for all structures of JSON.



const data = foo: 1, bar: magic: , baz: woo: some: test: ['result'] 

const getField = (fields, item) =>
if (fields.length < 1)
return item

else
return fields.split('.').reduce((a, b) => a[b], item)



const checkObj = obj => Object.entries(obj).map(
([key, value]) =>
return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)

)

const findArr = obj =>
let checked = ''
let itm = checkObj(getField(checked, data))

while (itm.some(e => !e.arr))
itm = checkObj(getField(checked, data))
const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
// Find longest key
checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
// Track keys checked

return checked
;


Test it out:
https://codesandbox.io/s/lr0ry18j2l







share|improve this question



























    up vote
    0
    down vote

    favorite












    I am recursively checking an object to:



    • Find the largest JSON object

    • Find the array within it

    It has to be rather flexible, as it should work with any structure of JSON data.



    The below works, but it isn't flexible for all structures of JSON.



    const data = foo: 1, bar: magic: , baz: woo: some: test: ['result'] 

    const getField = (fields, item) =>
    if (fields.length < 1)
    return item

    else
    return fields.split('.').reduce((a, b) => a[b], item)



    const checkObj = obj => Object.entries(obj).map(
    ([key, value]) =>
    return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)

    )

    const findArr = obj =>
    let checked = ''
    let itm = checkObj(getField(checked, data))

    while (itm.some(e => !e.arr))
    itm = checkObj(getField(checked, data))
    const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
    // Find longest key
    checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
    // Track keys checked

    return checked
    ;


    Test it out:
    https://codesandbox.io/s/lr0ry18j2l







    share|improve this question























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I am recursively checking an object to:



      • Find the largest JSON object

      • Find the array within it

      It has to be rather flexible, as it should work with any structure of JSON data.



      The below works, but it isn't flexible for all structures of JSON.



      const data = foo: 1, bar: magic: , baz: woo: some: test: ['result'] 

      const getField = (fields, item) =>
      if (fields.length < 1)
      return item

      else
      return fields.split('.').reduce((a, b) => a[b], item)



      const checkObj = obj => Object.entries(obj).map(
      ([key, value]) =>
      return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)

      )

      const findArr = obj =>
      let checked = ''
      let itm = checkObj(getField(checked, data))

      while (itm.some(e => !e.arr))
      itm = checkObj(getField(checked, data))
      const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
      // Find longest key
      checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
      // Track keys checked

      return checked
      ;


      Test it out:
      https://codesandbox.io/s/lr0ry18j2l







      share|improve this question













      I am recursively checking an object to:



      • Find the largest JSON object

      • Find the array within it

      It has to be rather flexible, as it should work with any structure of JSON data.



      The below works, but it isn't flexible for all structures of JSON.



      const data = foo: 1, bar: magic: , baz: woo: some: test: ['result'] 

      const getField = (fields, item) =>
      if (fields.length < 1)
      return item

      else
      return fields.split('.').reduce((a, b) => a[b], item)



      const checkObj = obj => Object.entries(obj).map(
      ([key, value]) =>
      return len: JSON.stringify(value).length, key: key, arr: Array.isArray(value)

      )

      const findArr = obj =>
      let checked = ''
      let itm = checkObj(getField(checked, data))

      while (itm.some(e => !e.arr))
      itm = checkObj(getField(checked, data))
      const longestObj = itm.map(e => e).sort((a, b) => b.len - a.len)[0].key
      // Find longest key
      checked = checked.length > 1 ? `$checked.$longestObj` : longestObj
      // Track keys checked

      return checked
      ;


      Test it out:
      https://codesandbox.io/s/lr0ry18j2l









      share|improve this question












      share|improve this question




      share|improve this question








      edited Apr 17 at 13:15
























      asked Apr 17 at 12:45









      Ycon

      1467




      1467

























          active

          oldest

          votes











          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
          );
          );
          , "mathjax-editing");

          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: "196"
          ;
          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',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );








           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192286%2frecursively-check-object-for-first-array%23new-answer', 'question_page');

          );

          Post as a guest



































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes










           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f192286%2frecursively-check-object-for-first-array%23new-answer', 'question_page');

          );

          Post as a guest













































































          Popular posts from this blog

          Greedy Best First Search implementation in Rust

          Function to Return a JSON Like Objects Using VBA Collections and Arrays

          C++11 CLH Lock Implementation