Filter repeated keys










1















Please I'm new to this. I have searched, what I saw left me confused.



How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.



I want to have it like arrayA



let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]

let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]









share|improve this question

















  • 1





    would you like to go from a to b or from b to a? btw, what have you tried?

    – Nina Scholz
    Nov 16 '18 at 8:44












  • I want to go from a to b

    – obi patrick
    Nov 16 '18 at 8:47











  • What I tried was using variable as the key

    – obi patrick
    Nov 16 '18 at 8:49















1















Please I'm new to this. I have searched, what I saw left me confused.



How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.



I want to have it like arrayA



let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]

let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]









share|improve this question

















  • 1





    would you like to go from a to b or from b to a? btw, what have you tried?

    – Nina Scholz
    Nov 16 '18 at 8:44












  • I want to go from a to b

    – obi patrick
    Nov 16 '18 at 8:47











  • What I tried was using variable as the key

    – obi patrick
    Nov 16 '18 at 8:49













1












1








1








Please I'm new to this. I have searched, what I saw left me confused.



How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.



I want to have it like arrayA



let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]

let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]









share|improve this question














Please I'm new to this. I have searched, what I saw left me confused.



How can I reduce duplicated items in an array and add the values of the duplicated key to the unique one like below.



I want to have it like arrayA



let arrayA = [
name: "temperature", data: "2000-09-26": 82 ,
name: "temperature", data: "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79 ,
name: "frequency", data: "2004-01-17": 16 ,
name: "current", data: "1997-02-14": 65
]

let arrayB = [
name: "temperature", data: "2000-09-26": 82, "1997-03-08": 71 ,
name: "current", data: "1993-08-11": 79, "1997-02-14": 65 ,
name: "frequency", data: "2004-01-17": 16
]






javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 8:42









obi patrickobi patrick

214




214







  • 1





    would you like to go from a to b or from b to a? btw, what have you tried?

    – Nina Scholz
    Nov 16 '18 at 8:44












  • I want to go from a to b

    – obi patrick
    Nov 16 '18 at 8:47











  • What I tried was using variable as the key

    – obi patrick
    Nov 16 '18 at 8:49












  • 1





    would you like to go from a to b or from b to a? btw, what have you tried?

    – Nina Scholz
    Nov 16 '18 at 8:44












  • I want to go from a to b

    – obi patrick
    Nov 16 '18 at 8:47











  • What I tried was using variable as the key

    – obi patrick
    Nov 16 '18 at 8:49







1




1





would you like to go from a to b or from b to a? btw, what have you tried?

– Nina Scholz
Nov 16 '18 at 8:44






would you like to go from a to b or from b to a? btw, what have you tried?

– Nina Scholz
Nov 16 '18 at 8:44














I want to go from a to b

– obi patrick
Nov 16 '18 at 8:47





I want to go from a to b

– obi patrick
Nov 16 '18 at 8:47













What I tried was using variable as the key

– obi patrick
Nov 16 '18 at 8:49





What I tried was using variable as the key

– obi patrick
Nov 16 '18 at 8:49












3 Answers
3






active

oldest

votes


















2














You can use array#reduce and Object.values(). Create a hash of name and add the data value for a given name. Then get the values from the object using Object.values().






let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
65 ],
result = Object.values(arrayA.reduce((r,name, data) => ,));
console.log(result);








share|improve this answer






























    0














    Here you are



    let arrayB = arrayA.reduce((acc, curr) => 

    const found = acc.find(i => i.name === curr.name)
    if (found)
    // If this object is already there, merge their `data` property
    Object.assign(found.data, curr.data)
    else
    // Otherwise add to the accumulator (`acc`)
    acc.push(curr)


    return acc
    , )


    Reference to the reduce function:
    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce






    share|improve this answer






























      0

















      var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
      var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

      return _.reduce(list,function(nObj,obj)
      if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
      nObj.data=_.assign(nObj.data,obj.data)
      else
      nObj=obj

      return nObj
      ,)
      )
      console.log(arrayB)

      <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>








      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%2f53334211%2ffilter-repeated-keys%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









        2














        You can use array#reduce and Object.values(). Create a hash of name and add the data value for a given name. Then get the values from the object using Object.values().






        let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
        65 ],
        result = Object.values(arrayA.reduce((r,name, data) => ,));
        console.log(result);








        share|improve this answer



























          2














          You can use array#reduce and Object.values(). Create a hash of name and add the data value for a given name. Then get the values from the object using Object.values().






          let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
          65 ],
          result = Object.values(arrayA.reduce((r,name, data) => ,));
          console.log(result);








          share|improve this answer

























            2












            2








            2







            You can use array#reduce and Object.values(). Create a hash of name and add the data value for a given name. Then get the values from the object using Object.values().






            let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
            65 ],
            result = Object.values(arrayA.reduce((r,name, data) => ,));
            console.log(result);








            share|improve this answer













            You can use array#reduce and Object.values(). Create a hash of name and add the data value for a given name. Then get the values from the object using Object.values().






            let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
            65 ],
            result = Object.values(arrayA.reduce((r,name, data) => ,));
            console.log(result);








            let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
            65 ],
            result = Object.values(arrayA.reduce((r,name, data) => ,));
            console.log(result);





            let arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14":
            65 ],
            result = Object.values(arrayA.reduce((r,name, data) => ,));
            console.log(result);






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 8:49









            Hassan ImamHassan Imam

            12k31532




            12k31532























                0














                Here you are



                let arrayB = arrayA.reduce((acc, curr) => 

                const found = acc.find(i => i.name === curr.name)
                if (found)
                // If this object is already there, merge their `data` property
                Object.assign(found.data, curr.data)
                else
                // Otherwise add to the accumulator (`acc`)
                acc.push(curr)


                return acc
                , )


                Reference to the reduce function:
                https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce






                share|improve this answer



























                  0














                  Here you are



                  let arrayB = arrayA.reduce((acc, curr) => 

                  const found = acc.find(i => i.name === curr.name)
                  if (found)
                  // If this object is already there, merge their `data` property
                  Object.assign(found.data, curr.data)
                  else
                  // Otherwise add to the accumulator (`acc`)
                  acc.push(curr)


                  return acc
                  , )


                  Reference to the reduce function:
                  https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce






                  share|improve this answer

























                    0












                    0








                    0







                    Here you are



                    let arrayB = arrayA.reduce((acc, curr) => 

                    const found = acc.find(i => i.name === curr.name)
                    if (found)
                    // If this object is already there, merge their `data` property
                    Object.assign(found.data, curr.data)
                    else
                    // Otherwise add to the accumulator (`acc`)
                    acc.push(curr)


                    return acc
                    , )


                    Reference to the reduce function:
                    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce






                    share|improve this answer













                    Here you are



                    let arrayB = arrayA.reduce((acc, curr) => 

                    const found = acc.find(i => i.name === curr.name)
                    if (found)
                    // If this object is already there, merge their `data` property
                    Object.assign(found.data, curr.data)
                    else
                    // Otherwise add to the accumulator (`acc`)
                    acc.push(curr)


                    return acc
                    , )


                    Reference to the reduce function:
                    https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 8:53









                    Nurbol AlpysbayevNurbol Alpysbayev

                    4,8091533




                    4,8091533





















                        0

















                        var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                        var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                        return _.reduce(list,function(nObj,obj)
                        if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                        nObj.data=_.assign(nObj.data,obj.data)
                        else
                        nObj=obj

                        return nObj
                        ,)
                        )
                        console.log(arrayB)

                        <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>








                        share|improve this answer



























                          0

















                          var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                          var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                          return _.reduce(list,function(nObj,obj)
                          if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                          nObj.data=_.assign(nObj.data,obj.data)
                          else
                          nObj=obj

                          return nObj
                          ,)
                          )
                          console.log(arrayB)

                          <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>








                          share|improve this answer

























                            0












                            0








                            0










                            var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                            var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                            return _.reduce(list,function(nObj,obj)
                            if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                            nObj.data=_.assign(nObj.data,obj.data)
                            else
                            nObj=obj

                            return nObj
                            ,)
                            )
                            console.log(arrayB)

                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>








                            share|improve this answer
















                            var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                            var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                            return _.reduce(list,function(nObj,obj)
                            if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                            nObj.data=_.assign(nObj.data,obj.data)
                            else
                            nObj=obj

                            return nObj
                            ,)
                            )
                            console.log(arrayB)

                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>








                            var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                            var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                            return _.reduce(list,function(nObj,obj)
                            if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                            nObj.data=_.assign(nObj.data,obj.data)
                            else
                            nObj=obj

                            return nObj
                            ,)
                            )
                            console.log(arrayB)

                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>





                            var arrayA = [ name: "temperature", data: "2000-09-26": 82 , name: "temperature", data: "1997-03-08": 71 , name: "current", data: "1993-08-11": 79 , name: "frequency", data: "2004-01-17": 16 , name: "current", data: "1997-02-14": 65 ]
                            var arrayB=_.map(_.groupBy(arrayA, 'name'),function(list)

                            return _.reduce(list,function(nObj,obj)
                            if(nObj.data && _.difference(_.keys(nObj.data),_.keys(obj.data)))
                            nObj.data=_.assign(nObj.data,obj.data)
                            else
                            nObj=obj

                            return nObj
                            ,)
                            )
                            console.log(arrayB)

                            <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.5.1/lodash.js"></script>






                            share|improve this answer












                            share|improve this answer



                            share|improve this answer










                            answered Nov 16 '18 at 9:35









                            Pritam JanaPritam Jana

                            10913




                            10913



























                                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%2f53334211%2ffilter-repeated-keys%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

                                Top Tejano songwriter Luis Silva dead of heart attack at 64

                                政党

                                天津地下鉄3号線