Determine Array include value in Another Array












0















I want to check if arr1 include arr2



i have try use .some and includes but it not return the right answer for me



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]


my expected



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]









share|improve this question

























  • Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

    – Baruch
    Jan 2 at 17:29











  • So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

    – adiga
    Jan 2 at 17:36











  • please add the wanted result as well.

    – Nina Scholz
    Jan 2 at 17:39











  • i have updated , so sorry

    – Phạm Minh Tân
    Jan 2 at 17:56
















0















I want to check if arr1 include arr2



i have try use .some and includes but it not return the right answer for me



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]


my expected



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]









share|improve this question

























  • Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

    – Baruch
    Jan 2 at 17:29











  • So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

    – adiga
    Jan 2 at 17:36











  • please add the wanted result as well.

    – Nina Scholz
    Jan 2 at 17:39











  • i have updated , so sorry

    – Phạm Minh Tân
    Jan 2 at 17:56














0












0








0








I want to check if arr1 include arr2



i have try use .some and includes but it not return the right answer for me



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]


my expected



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]









share|improve this question
















I want to check if arr1 include arr2



i have try use .some and includes but it not return the right answer for me



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'}]
arr2=[{Name:'John',Age:18},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]


my expected



arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19},{Name:'Steve',Age:'21'},{Name:'Hana',Age:19},{Name:'Josh',Age:20}]






javascript






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 17:56







Phạm Minh Tân

















asked Jan 2 at 17:25









Phạm Minh TânPhạm Minh Tân

295




295













  • Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

    – Baruch
    Jan 2 at 17:29











  • So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

    – adiga
    Jan 2 at 17:36











  • please add the wanted result as well.

    – Nina Scholz
    Jan 2 at 17:39











  • i have updated , so sorry

    – Phạm Minh Tân
    Jan 2 at 17:56



















  • Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

    – Baruch
    Jan 2 at 17:29











  • So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

    – adiga
    Jan 2 at 17:36











  • please add the wanted result as well.

    – Nina Scholz
    Jan 2 at 17:39











  • i have updated , so sorry

    – Phạm Minh Tân
    Jan 2 at 17:56

















Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

– Baruch
Jan 2 at 17:29





Seems like you're looking for someone to write the code for you. It'll be better if you post any solutions you've tried since SO is not for people to write the code for you.

– Baruch
Jan 2 at 17:29













So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

– adiga
Jan 2 at 17:36





So which one is it? You want the output to be just a true or false OR you want to merge the arrays?

– adiga
Jan 2 at 17:36













please add the wanted result as well.

– Nina Scholz
Jan 2 at 17:39





please add the wanted result as well.

– Nina Scholz
Jan 2 at 17:39













i have updated , so sorry

– Phạm Minh Tân
Jan 2 at 17:56





i have updated , so sorry

– Phạm Minh Tân
Jan 2 at 17:56












3 Answers
3






active

oldest

votes


















0














You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.






let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
arr2=[{Name:'John',Age:18}],
result = arr2.every(({Name, Age}) => {
return arr1.some(o => o.Name === Name && o.Age === Age);
});
console.log(result);








share|improve this answer































    0














    Since they are not the same object just objects with same values you have to compare each value.






    const arr1 = [{
    Name: 'John',
    Age: 18
    }, {
    Name: 'Leona',
    Age: 19
    }];
    const arr2 = [{
    Name: 'John',
    Age: 18
    }];

    const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
    console.log(check);








    share|improve this answer































      0














      You could reduce the first array and check if Name is not in the first array, then add it to the result set.






      var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
      array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

      array2.reduce((r, o) => {
      if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
      return r;
      }, array1);

      console.log(array1);

      .as-console-wrapper { max-height: 100% !important; top: 0; }








      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%2f54010620%2fdetermine-array-include-value-in-another-array%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        3 Answers
        3






        active

        oldest

        votes








        3 Answers
        3






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.






        let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
        arr2=[{Name:'John',Age:18}],
        result = arr2.every(({Name, Age}) => {
        return arr1.some(o => o.Name === Name && o.Age === Age);
        });
        console.log(result);








        share|improve this answer




























          0














          You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.






          let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
          arr2=[{Name:'John',Age:18}],
          result = arr2.every(({Name, Age}) => {
          return arr1.some(o => o.Name === Name && o.Age === Age);
          });
          console.log(result);








          share|improve this answer


























            0












            0








            0







            You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.






            let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
            arr2=[{Name:'John',Age:18}],
            result = arr2.every(({Name, Age}) => {
            return arr1.some(o => o.Name === Name && o.Age === Age);
            });
            console.log(result);








            share|improve this answer













            You can use array#every with array#some. Iterate through each object in arr2 and check if the object exist in arr1.






            let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
            arr2=[{Name:'John',Age:18}],
            result = arr2.every(({Name, Age}) => {
            return arr1.some(o => o.Name === Name && o.Age === Age);
            });
            console.log(result);








            let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
            arr2=[{Name:'John',Age:18}],
            result = arr2.every(({Name, Age}) => {
            return arr1.some(o => o.Name === Name && o.Age === Age);
            });
            console.log(result);





            let arr1=[{Name:'John',Age:18},{Name:'Leona',Age:19}],
            arr2=[{Name:'John',Age:18}],
            result = arr2.every(({Name, Age}) => {
            return arr1.some(o => o.Name === Name && o.Age === Age);
            });
            console.log(result);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Jan 2 at 17:29









            Hassan ImamHassan Imam

            11.9k31532




            11.9k31532

























                0














                Since they are not the same object just objects with same values you have to compare each value.






                const arr1 = [{
                Name: 'John',
                Age: 18
                }, {
                Name: 'Leona',
                Age: 19
                }];
                const arr2 = [{
                Name: 'John',
                Age: 18
                }];

                const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                console.log(check);








                share|improve this answer




























                  0














                  Since they are not the same object just objects with same values you have to compare each value.






                  const arr1 = [{
                  Name: 'John',
                  Age: 18
                  }, {
                  Name: 'Leona',
                  Age: 19
                  }];
                  const arr2 = [{
                  Name: 'John',
                  Age: 18
                  }];

                  const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                  console.log(check);








                  share|improve this answer


























                    0












                    0








                    0







                    Since they are not the same object just objects with same values you have to compare each value.






                    const arr1 = [{
                    Name: 'John',
                    Age: 18
                    }, {
                    Name: 'Leona',
                    Age: 19
                    }];
                    const arr2 = [{
                    Name: 'John',
                    Age: 18
                    }];

                    const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                    console.log(check);








                    share|improve this answer













                    Since they are not the same object just objects with same values you have to compare each value.






                    const arr1 = [{
                    Name: 'John',
                    Age: 18
                    }, {
                    Name: 'Leona',
                    Age: 19
                    }];
                    const arr2 = [{
                    Name: 'John',
                    Age: 18
                    }];

                    const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                    console.log(check);








                    const arr1 = [{
                    Name: 'John',
                    Age: 18
                    }, {
                    Name: 'Leona',
                    Age: 19
                    }];
                    const arr2 = [{
                    Name: 'John',
                    Age: 18
                    }];

                    const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                    console.log(check);





                    const arr1 = [{
                    Name: 'John',
                    Age: 18
                    }, {
                    Name: 'Leona',
                    Age: 19
                    }];
                    const arr2 = [{
                    Name: 'John',
                    Age: 18
                    }];

                    const check = arr1.some(e1 => arr2.some(e2 => e1.Name === e2.Name && e1.Age === e2.Age));
                    console.log(check);






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 2 at 17:31









                    Sebastian SpeitelSebastian Speitel

                    4,5882727




                    4,5882727























                        0














                        You could reduce the first array and check if Name is not in the first array, then add it to the result set.






                        var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                        array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                        array2.reduce((r, o) => {
                        if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                        return r;
                        }, array1);

                        console.log(array1);

                        .as-console-wrapper { max-height: 100% !important; top: 0; }








                        share|improve this answer




























                          0














                          You could reduce the first array and check if Name is not in the first array, then add it to the result set.






                          var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                          array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                          array2.reduce((r, o) => {
                          if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                          return r;
                          }, array1);

                          console.log(array1);

                          .as-console-wrapper { max-height: 100% !important; top: 0; }








                          share|improve this answer


























                            0












                            0








                            0







                            You could reduce the first array and check if Name is not in the first array, then add it to the result set.






                            var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                            array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                            array2.reduce((r, o) => {
                            if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                            return r;
                            }, array1);

                            console.log(array1);

                            .as-console-wrapper { max-height: 100% !important; top: 0; }








                            share|improve this answer













                            You could reduce the first array and check if Name is not in the first array, then add it to the result set.






                            var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                            array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                            array2.reduce((r, o) => {
                            if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                            return r;
                            }, array1);

                            console.log(array1);

                            .as-console-wrapper { max-height: 100% !important; top: 0; }








                            var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                            array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                            array2.reduce((r, o) => {
                            if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                            return r;
                            }, array1);

                            console.log(array1);

                            .as-console-wrapper { max-height: 100% !important; top: 0; }





                            var array1 = [{ Name: 'John', Age: 18 }, { Name: 'Leona', Age: 19 }, { Name: 'Steve', Age: '21' }],
                            array2 = [{ Name: 'John', Age: 18 }, { Name: 'Hana', Age: 19 }, { Name: 'Josh', Age: 20 }];

                            array2.reduce((r, o) => {
                            if (!r.some(({ Name }) => o.Name === Name)) r.push(o);
                            return r;
                            }, array1);

                            console.log(array1);

                            .as-console-wrapper { max-height: 100% !important; top: 0; }






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Jan 2 at 21:55









                            Nina ScholzNina Scholz

                            191k15103175




                            191k15103175






























                                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%2f54010620%2fdetermine-array-include-value-in-another-array%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

                                Mossoró

                                Error while reading .h5 file using the rhdf5 package in R

                                Pushsharp Apns notification error: 'InvalidToken'