JS - Comparing 2 Array of Objects












0















I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes



Please refer to the image below



enter image description here



As you can see, on the 2nd record value 1:Three should be placed on col 1



This is my array of objects:



const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'}],
[{labelId:'2', value:'Four'}]
]


I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>



So as one of the suggestion, here's my attempt:



    const labelCols = cols.map(col => {
const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
console.log(col)
if(arrayOfMatch !== 1) { //matching label exists
return { ...col, ...labels[arrayOfMatch] };
} else {
return { ...col, label: '' }; // insert empty label
}
});
console.log(labelCols)


I'm hopping my JSON will shape up like this:



const cols = [
[{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
[{labelId:'1', value:'Three'},{}],
[{},{labelId:'2', value:'Four'}]
]


Let me know if anyone can come up with better solution



Thanks in advance!










share|improve this question





























    0















    I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes



    Please refer to the image below



    enter image description here



    As you can see, on the 2nd record value 1:Three should be placed on col 1



    This is my array of objects:



    const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
    const cols = [
    [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
    [{labelId:'1', value:'Three'}],
    [{labelId:'2', value:'Four'}]
    ]


    I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>



    So as one of the suggestion, here's my attempt:



        const labelCols = cols.map(col => {
    const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
    console.log(col)
    if(arrayOfMatch !== 1) { //matching label exists
    return { ...col, ...labels[arrayOfMatch] };
    } else {
    return { ...col, label: '' }; // insert empty label
    }
    });
    console.log(labelCols)


    I'm hopping my JSON will shape up like this:



    const cols = [
    [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
    [{labelId:'1', value:'Three'},{}],
    [{},{labelId:'2', value:'Four'}]
    ]


    Let me know if anyone can come up with better solution



    Thanks in advance!










    share|improve this question



























      0












      0








      0








      I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes



      Please refer to the image below



      enter image description here



      As you can see, on the 2nd record value 1:Three should be placed on col 1



      This is my array of objects:



      const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
      const cols = [
      [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
      [{labelId:'1', value:'Three'}],
      [{labelId:'2', value:'Four'}]
      ]


      I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>



      So as one of the suggestion, here's my attempt:



          const labelCols = cols.map(col => {
      const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
      console.log(col)
      if(arrayOfMatch !== 1) { //matching label exists
      return { ...col, ...labels[arrayOfMatch] };
      } else {
      return { ...col, label: '' }; // insert empty label
      }
      });
      console.log(labelCols)


      I'm hopping my JSON will shape up like this:



      const cols = [
      [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
      [{labelId:'1', value:'Three'},{}],
      [{},{labelId:'2', value:'Four'}]
      ]


      Let me know if anyone can come up with better solution



      Thanks in advance!










      share|improve this question
















      I'm creating table view based on a JSON feed, so one of the biggest is while my labels(row) stays consistent, the table data on my cols changes



      Please refer to the image below



      enter image description here



      As you can see, on the 2nd record value 1:Three should be placed on col 1



      This is my array of objects:



      const label = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}]
      const cols = [
      [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
      [{labelId:'1', value:'Three'}],
      [{labelId:'2', value:'Four'}]
      ]


      I wanna be able to compare them so I can indicate which one that doesn't have corresponding label. Thus i'll be able to replace them by empty <td></td>



      So as one of the suggestion, here's my attempt:



          const labelCols = cols.map(col => {
      const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
      console.log(col)
      if(arrayOfMatch !== 1) { //matching label exists
      return { ...col, ...labels[arrayOfMatch] };
      } else {
      return { ...col, label: '' }; // insert empty label
      }
      });
      console.log(labelCols)


      I'm hopping my JSON will shape up like this:



      const cols = [
      [{labelId:'1', value:'One'},{labelId:'2', value:'Two'}],
      [{labelId:'1', value:'Three'},{}],
      [{},{labelId:'2', value:'Four'}]
      ]


      Let me know if anyone can come up with better solution



      Thanks in advance!







      javascript reactjs loops oop ecmascript-6






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 23 '18 at 7:04







      Amy Lee

















      asked Nov 23 '18 at 2:56









      Amy LeeAmy Lee

      149111




      149111
























          2 Answers
          2






          active

          oldest

          votes


















          1














          I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:



            const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
          const cols = [{labelId:'1', value:'One'},
          {labelId:'2', value:'Two'},
          {labelId:'3', value:'Three'},
          {labelId:'4', value:'Four'},
          ];

          const labelCols = cols.map(col => {
          const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
          if(arrayOfMatch !== -1) { //matching label exists
          return { ...col, ...labels[arrayOfMatch] };
          } else {
          return { ...col, label: '' }; // insert empty label
          }
          });


          labelCols will be containing something like this:



          labelCols = [{labelId:'1', value:'One', label: 'test1'},
          {labelId:'2', value:'Two', label: 'test2'},
          {labelId:'3', value:'Three', label: ''},
          {labelId:'4', value:'Four', label: ''},
          ];


          Then, if you want to render td elements with labels, do this:



            <table>
          //other codes
          <tr>
          {labelCols.map(item => <td>{item.label}</td>)}
          </tr>
          //other codes
          </table>





          share|improve this answer
























          • Awesome! yeah that's what i want, lemme try it

            – Amy Lee
            Nov 23 '18 at 3:29











          • Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

            – Amy Lee
            Nov 23 '18 at 6:26











          • Edit your question above to make it clear.

            – Sandy Brutas
            Nov 23 '18 at 6:35











          • I just have, let me know if it's still unclear. Thanks again for your help

            – Amy Lee
            Nov 23 '18 at 7:05



















          0














          // For comparing two array, array must be with same type of same length
          var isEqualArray = labels.map((label)=>{
          cols.map((col)=> label.labelId === col.labelId)
          }).filer((item) => item === true)
          .length === labels.length;


          if (isEqualArray ) {
          // Equal
          }





          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%2f53440147%2fjs-comparing-2-array-of-objects%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:



              const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
            const cols = [{labelId:'1', value:'One'},
            {labelId:'2', value:'Two'},
            {labelId:'3', value:'Three'},
            {labelId:'4', value:'Four'},
            ];

            const labelCols = cols.map(col => {
            const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
            if(arrayOfMatch !== -1) { //matching label exists
            return { ...col, ...labels[arrayOfMatch] };
            } else {
            return { ...col, label: '' }; // insert empty label
            }
            });


            labelCols will be containing something like this:



            labelCols = [{labelId:'1', value:'One', label: 'test1'},
            {labelId:'2', value:'Two', label: 'test2'},
            {labelId:'3', value:'Three', label: ''},
            {labelId:'4', value:'Four', label: ''},
            ];


            Then, if you want to render td elements with labels, do this:



              <table>
            //other codes
            <tr>
            {labelCols.map(item => <td>{item.label}</td>)}
            </tr>
            //other codes
            </table>





            share|improve this answer
























            • Awesome! yeah that's what i want, lemme try it

              – Amy Lee
              Nov 23 '18 at 3:29











            • Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

              – Amy Lee
              Nov 23 '18 at 6:26











            • Edit your question above to make it clear.

              – Sandy Brutas
              Nov 23 '18 at 6:35











            • I just have, let me know if it's still unclear. Thanks again for your help

              – Amy Lee
              Nov 23 '18 at 7:05
















            1














            I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:



              const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
            const cols = [{labelId:'1', value:'One'},
            {labelId:'2', value:'Two'},
            {labelId:'3', value:'Three'},
            {labelId:'4', value:'Four'},
            ];

            const labelCols = cols.map(col => {
            const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
            if(arrayOfMatch !== -1) { //matching label exists
            return { ...col, ...labels[arrayOfMatch] };
            } else {
            return { ...col, label: '' }; // insert empty label
            }
            });


            labelCols will be containing something like this:



            labelCols = [{labelId:'1', value:'One', label: 'test1'},
            {labelId:'2', value:'Two', label: 'test2'},
            {labelId:'3', value:'Three', label: ''},
            {labelId:'4', value:'Four', label: ''},
            ];


            Then, if you want to render td elements with labels, do this:



              <table>
            //other codes
            <tr>
            {labelCols.map(item => <td>{item.label}</td>)}
            </tr>
            //other codes
            </table>





            share|improve this answer
























            • Awesome! yeah that's what i want, lemme try it

              – Amy Lee
              Nov 23 '18 at 3:29











            • Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

              – Amy Lee
              Nov 23 '18 at 6:26











            • Edit your question above to make it clear.

              – Sandy Brutas
              Nov 23 '18 at 6:35











            • I just have, let me know if it's still unclear. Thanks again for your help

              – Amy Lee
              Nov 23 '18 at 7:05














            1












            1








            1







            I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:



              const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
            const cols = [{labelId:'1', value:'One'},
            {labelId:'2', value:'Two'},
            {labelId:'3', value:'Three'},
            {labelId:'4', value:'Four'},
            ];

            const labelCols = cols.map(col => {
            const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
            if(arrayOfMatch !== -1) { //matching label exists
            return { ...col, ...labels[arrayOfMatch] };
            } else {
            return { ...col, label: '' }; // insert empty label
            }
            });


            labelCols will be containing something like this:



            labelCols = [{labelId:'1', value:'One', label: 'test1'},
            {labelId:'2', value:'Two', label: 'test2'},
            {labelId:'3', value:'Three', label: ''},
            {labelId:'4', value:'Four', label: ''},
            ];


            Then, if you want to render td elements with labels, do this:



              <table>
            //other codes
            <tr>
            {labelCols.map(item => <td>{item.label}</td>)}
            </tr>
            //other codes
            </table>





            share|improve this answer













            I think you're trying to "merge" your objects then generate table rows out of them. Considering your objects are actually like these:



              const labels = [{label: 'test1', labelId:'1'},{label: 'test2', labelId:'2'}];
            const cols = [{labelId:'1', value:'One'},
            {labelId:'2', value:'Two'},
            {labelId:'3', value:'Three'},
            {labelId:'4', value:'Four'},
            ];

            const labelCols = cols.map(col => {
            const arrayOfMatch = labels.findIndex(label => label.labelId === col.labelId);
            if(arrayOfMatch !== -1) { //matching label exists
            return { ...col, ...labels[arrayOfMatch] };
            } else {
            return { ...col, label: '' }; // insert empty label
            }
            });


            labelCols will be containing something like this:



            labelCols = [{labelId:'1', value:'One', label: 'test1'},
            {labelId:'2', value:'Two', label: 'test2'},
            {labelId:'3', value:'Three', label: ''},
            {labelId:'4', value:'Four', label: ''},
            ];


            Then, if you want to render td elements with labels, do this:



              <table>
            //other codes
            <tr>
            {labelCols.map(item => <td>{item.label}</td>)}
            </tr>
            //other codes
            </table>






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 23 '18 at 3:22









            Sandy BrutasSandy Brutas

            494314




            494314













            • Awesome! yeah that's what i want, lemme try it

              – Amy Lee
              Nov 23 '18 at 3:29











            • Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

              – Amy Lee
              Nov 23 '18 at 6:26











            • Edit your question above to make it clear.

              – Sandy Brutas
              Nov 23 '18 at 6:35











            • I just have, let me know if it's still unclear. Thanks again for your help

              – Amy Lee
              Nov 23 '18 at 7:05



















            • Awesome! yeah that's what i want, lemme try it

              – Amy Lee
              Nov 23 '18 at 3:29











            • Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

              – Amy Lee
              Nov 23 '18 at 6:26











            • Edit your question above to make it clear.

              – Sandy Brutas
              Nov 23 '18 at 6:35











            • I just have, let me know if it's still unclear. Thanks again for your help

              – Amy Lee
              Nov 23 '18 at 7:05

















            Awesome! yeah that's what i want, lemme try it

            – Amy Lee
            Nov 23 '18 at 3:29





            Awesome! yeah that's what i want, lemme try it

            – Amy Lee
            Nov 23 '18 at 3:29













            Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

            – Amy Lee
            Nov 23 '18 at 6:26





            Hey Sandy, thanks for the solution, i think im almost there but i realised that i've pasted in the wrong code, so here's my issue i'm able to pump in the label into each object inside an array, but i can't seems to pump in dummy object just to ensure it fill in void <td></td>, just wondering if you'll be able to help me to solve my additional issue, thanks heaps again!

            – Amy Lee
            Nov 23 '18 at 6:26













            Edit your question above to make it clear.

            – Sandy Brutas
            Nov 23 '18 at 6:35





            Edit your question above to make it clear.

            – Sandy Brutas
            Nov 23 '18 at 6:35













            I just have, let me know if it's still unclear. Thanks again for your help

            – Amy Lee
            Nov 23 '18 at 7:05





            I just have, let me know if it's still unclear. Thanks again for your help

            – Amy Lee
            Nov 23 '18 at 7:05













            0














            // For comparing two array, array must be with same type of same length
            var isEqualArray = labels.map((label)=>{
            cols.map((col)=> label.labelId === col.labelId)
            }).filer((item) => item === true)
            .length === labels.length;


            if (isEqualArray ) {
            // Equal
            }





            share|improve this answer






























              0














              // For comparing two array, array must be with same type of same length
              var isEqualArray = labels.map((label)=>{
              cols.map((col)=> label.labelId === col.labelId)
              }).filer((item) => item === true)
              .length === labels.length;


              if (isEqualArray ) {
              // Equal
              }





              share|improve this answer




























                0












                0








                0







                // For comparing two array, array must be with same type of same length
                var isEqualArray = labels.map((label)=>{
                cols.map((col)=> label.labelId === col.labelId)
                }).filer((item) => item === true)
                .length === labels.length;


                if (isEqualArray ) {
                // Equal
                }





                share|improve this answer















                // For comparing two array, array must be with same type of same length
                var isEqualArray = labels.map((label)=>{
                cols.map((col)=> label.labelId === col.labelId)
                }).filer((item) => item === true)
                .length === labels.length;


                if (isEqualArray ) {
                // Equal
                }






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 23 '18 at 3:20

























                answered Nov 23 '18 at 3:12









                Atul BansodeAtul Bansode

                112




                112






























                    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%2f53440147%2fjs-comparing-2-array-of-objects%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







                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini