Finding an index in an array of objects with 3 possible outcomes

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
3
down vote

favorite












currentUserPosts will have a post object with an id that either matches a tempId, an Id, or nothing.



If there's a tempId match there won't be an id match and vice versa.



This code works but there's something about it I don't like. How can it be refactored?



let postIndex;
postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.tempId);
if (postIndex === -1)
postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.id);

if (postIndex === -1)
currentUserPosts.push(upsertParams);
else
currentUserPosts[postIndex] = upsertParams;



Complete function:



const upsertIntoApolloCache = (upsertParams) => 
try
const data = client.readQuery(
query: USER_POSTS_QUERY,
);
const currentUserPosts = data.currentUser.posts;

const postIndex = currentUserPosts.findIndex(
post => post.id === upsertParams.tempId catch (error)
console.log('!!ERROR in upsertIntoApolloCache!!', error);

;






share|improve this question



























    up vote
    3
    down vote

    favorite












    currentUserPosts will have a post object with an id that either matches a tempId, an Id, or nothing.



    If there's a tempId match there won't be an id match and vice versa.



    This code works but there's something about it I don't like. How can it be refactored?



    let postIndex;
    postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.tempId);
    if (postIndex === -1)
    postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.id);

    if (postIndex === -1)
    currentUserPosts.push(upsertParams);
    else
    currentUserPosts[postIndex] = upsertParams;



    Complete function:



    const upsertIntoApolloCache = (upsertParams) => 
    try
    const data = client.readQuery(
    query: USER_POSTS_QUERY,
    );
    const currentUserPosts = data.currentUser.posts;

    const postIndex = currentUserPosts.findIndex(
    post => post.id === upsertParams.tempId catch (error)
    console.log('!!ERROR in upsertIntoApolloCache!!', error);

    ;






    share|improve this question























      up vote
      3
      down vote

      favorite









      up vote
      3
      down vote

      favorite











      currentUserPosts will have a post object with an id that either matches a tempId, an Id, or nothing.



      If there's a tempId match there won't be an id match and vice versa.



      This code works but there's something about it I don't like. How can it be refactored?



      let postIndex;
      postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.tempId);
      if (postIndex === -1)
      postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.id);

      if (postIndex === -1)
      currentUserPosts.push(upsertParams);
      else
      currentUserPosts[postIndex] = upsertParams;



      Complete function:



      const upsertIntoApolloCache = (upsertParams) => 
      try
      const data = client.readQuery(
      query: USER_POSTS_QUERY,
      );
      const currentUserPosts = data.currentUser.posts;

      const postIndex = currentUserPosts.findIndex(
      post => post.id === upsertParams.tempId catch (error)
      console.log('!!ERROR in upsertIntoApolloCache!!', error);

      ;






      share|improve this question













      currentUserPosts will have a post object with an id that either matches a tempId, an Id, or nothing.



      If there's a tempId match there won't be an id match and vice versa.



      This code works but there's something about it I don't like. How can it be refactored?



      let postIndex;
      postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.tempId);
      if (postIndex === -1)
      postIndex = currentUserPosts.findIndex(post => post.id === upsertParams.id);

      if (postIndex === -1)
      currentUserPosts.push(upsertParams);
      else
      currentUserPosts[postIndex] = upsertParams;



      Complete function:



      const upsertIntoApolloCache = (upsertParams) => 
      try
      const data = client.readQuery(
      query: USER_POSTS_QUERY,
      );
      const currentUserPosts = data.currentUser.posts;

      const postIndex = currentUserPosts.findIndex(
      post => post.id === upsertParams.tempId catch (error)
      console.log('!!ERROR in upsertIntoApolloCache!!', error);

      ;








      share|improve this question












      share|improve this question




      share|improve this question








      edited Jun 24 at 16:17









      Jamal♦

      30.1k11114225




      30.1k11114225









      asked Jun 24 at 0:54









      GollyJer

      1628




      1628




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.



          let postIndex = currentUserPosts.findIndex(post => 
          return post.id === upsertParams.id );

          if (postIndex === -1)
          currentUserPosts.push(upsertParams);
          else
          currentUserPosts[postIndex] = upsertParams;



          You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.




          Based on updated question content:



          let currentUserPosts = 
          for (let post of data.currentUser.posts)
          currentUserPosts[post.id] = post



          This way, you'd only have to check against id using the in operation:



          let newId = upsertParams.id || upsertParams.tempId
          if (newId in currentUserPosts)
          // do unspeakable things





          share|improve this answer























          • Looks to me like you are missing a return in your findIndex method.
            – Gerrit0
            Jun 24 at 16:02










          • Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
            – GollyJer
            Jun 24 at 16:05










          • @Gerrit0 updated :) Thanks.
            – hjpotter92
            Jun 24 at 18:12










          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%2f197146%2ffinding-an-index-in-an-array-of-objects-with-3-possible-outcomes%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.



          let postIndex = currentUserPosts.findIndex(post => 
          return post.id === upsertParams.id );

          if (postIndex === -1)
          currentUserPosts.push(upsertParams);
          else
          currentUserPosts[postIndex] = upsertParams;



          You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.




          Based on updated question content:



          let currentUserPosts = 
          for (let post of data.currentUser.posts)
          currentUserPosts[post.id] = post



          This way, you'd only have to check against id using the in operation:



          let newId = upsertParams.id || upsertParams.tempId
          if (newId in currentUserPosts)
          // do unspeakable things





          share|improve this answer























          • Looks to me like you are missing a return in your findIndex method.
            – Gerrit0
            Jun 24 at 16:02










          • Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
            – GollyJer
            Jun 24 at 16:05










          • @Gerrit0 updated :) Thanks.
            – hjpotter92
            Jun 24 at 18:12














          up vote
          3
          down vote



          accepted










          You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.



          let postIndex = currentUserPosts.findIndex(post => 
          return post.id === upsertParams.id );

          if (postIndex === -1)
          currentUserPosts.push(upsertParams);
          else
          currentUserPosts[postIndex] = upsertParams;



          You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.




          Based on updated question content:



          let currentUserPosts = 
          for (let post of data.currentUser.posts)
          currentUserPosts[post.id] = post



          This way, you'd only have to check against id using the in operation:



          let newId = upsertParams.id || upsertParams.tempId
          if (newId in currentUserPosts)
          // do unspeakable things





          share|improve this answer























          • Looks to me like you are missing a return in your findIndex method.
            – Gerrit0
            Jun 24 at 16:02










          • Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
            – GollyJer
            Jun 24 at 16:05










          • @Gerrit0 updated :) Thanks.
            – hjpotter92
            Jun 24 at 18:12












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.



          let postIndex = currentUserPosts.findIndex(post => 
          return post.id === upsertParams.id );

          if (postIndex === -1)
          currentUserPosts.push(upsertParams);
          else
          currentUserPosts[postIndex] = upsertParams;



          You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.




          Based on updated question content:



          let currentUserPosts = 
          for (let post of data.currentUser.posts)
          currentUserPosts[post.id] = post



          This way, you'd only have to check against id using the in operation:



          let newId = upsertParams.id || upsertParams.tempId
          if (newId in currentUserPosts)
          // do unspeakable things





          share|improve this answer















          You can group the two conditions for finding into a single || condition, since the post.id can match only one of tempId or id.



          let postIndex = currentUserPosts.findIndex(post => 
          return post.id === upsertParams.id );

          if (postIndex === -1)
          currentUserPosts.push(upsertParams);
          else
          currentUserPosts[postIndex] = upsertParams;



          You could, if possible, also change the currentUserPosts from array to a dictionary mapping your post.id to the post itself. This entirely depends on your further usage of the currentUserPosts across application.




          Based on updated question content:



          let currentUserPosts = 
          for (let post of data.currentUser.posts)
          currentUserPosts[post.id] = post



          This way, you'd only have to check against id using the in operation:



          let newId = upsertParams.id || upsertParams.tempId
          if (newId in currentUserPosts)
          // do unspeakable things






          share|improve this answer















          share|improve this answer



          share|improve this answer








          edited Jun 24 at 18:17


























          answered Jun 24 at 9:52









          hjpotter92

          4,89611538




          4,89611538











          • Looks to me like you are missing a return in your findIndex method.
            – Gerrit0
            Jun 24 at 16:02










          • Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
            – GollyJer
            Jun 24 at 16:05










          • @Gerrit0 updated :) Thanks.
            – hjpotter92
            Jun 24 at 18:12
















          • Looks to me like you are missing a return in your findIndex method.
            – Gerrit0
            Jun 24 at 16:02










          • Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
            – GollyJer
            Jun 24 at 16:05










          • @Gerrit0 updated :) Thanks.
            – hjpotter92
            Jun 24 at 18:12















          Looks to me like you are missing a return in your findIndex method.
          – Gerrit0
          Jun 24 at 16:02




          Looks to me like you are missing a return in your findIndex method.
          – Gerrit0
          Jun 24 at 16:02












          Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
          – GollyJer
          Jun 24 at 16:05




          Ha!! That was an easy one. Thanks @hjpotter92! Regarding your second suggestion. Don't completely understand what you're getting at. I'll update my questions with the full function so you can see. It probably doesn't matter because I don't have control of the structure. It's a cache object provided by Apollo.
          – GollyJer
          Jun 24 at 16:05












          @Gerrit0 updated :) Thanks.
          – hjpotter92
          Jun 24 at 18:12




          @Gerrit0 updated :) Thanks.
          – hjpotter92
          Jun 24 at 18:12












           

          draft saved


          draft discarded


























           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f197146%2ffinding-an-index-in-an-array-of-objects-with-3-possible-outcomes%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