How can i get my required data from array of objects using javascript? [closed]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-5















I have array of objects like this



var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];


I want to have data like this



data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}


How can i get this data from given data?










share|improve this question















closed as too broad by jonrsharpe, Rob, R. Richards, SiddAjmera, gnat Nov 16 '18 at 14:05


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

    – jonrsharpe
    Nov 16 '18 at 12:06











  • @jonrsharpe what's the problem in expected output?

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:08











  • The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

    – jonrsharpe
    Nov 16 '18 at 12:10











  • yes, i updated post

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:12











  • Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

    – abdullahkady
    Nov 16 '18 at 12:13


















-5















I have array of objects like this



var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];


I want to have data like this



data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}


How can i get this data from given data?










share|improve this question















closed as too broad by jonrsharpe, Rob, R. Richards, SiddAjmera, gnat Nov 16 '18 at 14:05


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.














  • 2





    Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

    – jonrsharpe
    Nov 16 '18 at 12:06











  • @jonrsharpe what's the problem in expected output?

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:08











  • The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

    – jonrsharpe
    Nov 16 '18 at 12:10











  • yes, i updated post

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:12











  • Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

    – abdullahkady
    Nov 16 '18 at 12:13














-5












-5








-5








I have array of objects like this



var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];


I want to have data like this



data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}


How can i get this data from given data?










share|improve this question
















I have array of objects like this



var a = [
{'time' : 1539664755070,'T-1': 23 },
{'time' : 1539665095442,'H-1': 24 },
{'time' : 1539666489560,'T-1': 42 },
{'time' : 1539665095442,'H-1': 27 },
{'time': 1539671682230,'H-1': 40.45,'T-2': 33},
{'time': 1539671682230,'T-2': 30.45,'T-1': 65},
{'time': 1539671682230,'T-2': 42.45,'H-1': 11},
{'time': 1539671682230,'T-1': 50.45,'T-2': 85}
];


I want to have data like this



data : {
'T-1' : [23,42,50.45],
'T-2' : [33,30.45,85],
'H-1' : [24,27,40.45,11]
}


How can i get this data from given data?







javascript angular angular5






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 12:11







Ahsan Alam Siddiqui

















asked Nov 16 '18 at 12:05









Ahsan Alam SiddiquiAhsan Alam Siddiqui

219




219




closed as too broad by jonrsharpe, Rob, R. Richards, SiddAjmera, gnat Nov 16 '18 at 14:05


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.









closed as too broad by jonrsharpe, Rob, R. Richards, SiddAjmera, gnat Nov 16 '18 at 14:05


Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.










  • 2





    Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

    – jonrsharpe
    Nov 16 '18 at 12:06











  • @jonrsharpe what's the problem in expected output?

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:08











  • The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

    – jonrsharpe
    Nov 16 '18 at 12:10











  • yes, i updated post

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:12











  • Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

    – abdullahkady
    Nov 16 '18 at 12:13














  • 2





    Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

    – jonrsharpe
    Nov 16 '18 at 12:06











  • @jonrsharpe what's the problem in expected output?

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:08











  • The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

    – jonrsharpe
    Nov 16 '18 at 12:10











  • yes, i updated post

    – Ahsan Alam Siddiqui
    Nov 16 '18 at 12:12











  • Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

    – abdullahkady
    Nov 16 '18 at 12:13








2




2





Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

– jonrsharpe
Nov 16 '18 at 12:06





Your expected output isn't valid syntactically. What have you tried, and what exactly is the problem with it?

– jonrsharpe
Nov 16 '18 at 12:06













@jonrsharpe what's the problem in expected output?

– Ahsan Alam Siddiqui
Nov 16 '18 at 12:08





@jonrsharpe what's the problem in expected output?

– Ahsan Alam Siddiqui
Nov 16 '18 at 12:08













The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

– jonrsharpe
Nov 16 '18 at 12:10





The keys aren't valid identifiers; did you mean e.g. 'T-1': [...]?

– jonrsharpe
Nov 16 '18 at 12:10













yes, i updated post

– Ahsan Alam Siddiqui
Nov 16 '18 at 12:12





yes, i updated post

– Ahsan Alam Siddiqui
Nov 16 '18 at 12:12













Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

– abdullahkady
Nov 16 '18 at 12:13





Are the keys static? meaning you expect 'T-1', 'T-2', and 'H-1' every time?

– abdullahkady
Nov 16 '18 at 12:13












4 Answers
4






active

oldest

votes


















0














Here is a solution, if something is not clear, please lemme know:



const result = {};

a.forEach(object => {
// Loop on each entry in the original array
Object.entries(object).forEach(([key,value]) => {
// Loop through each entry of one entry of the original array
// (ex 'T-1', 'T-2')
if(key !== 'time') {
// It's a key that I'm interested in, so do logic
if(!result[key]) {
// If the key (example 'T-1') haven't been encountered yet,
// initialize it's entry in the result with an empty array
result[key] = ;
}
result[key].push(value);
}
});
});





share|improve this answer































    0














    Best use case for using Map instead of Key,Value arrays. Simplifies lot of things.






    share|improve this answer































      0














      You can use array#reduce with Object.values() to create a hash of your key and add the values corresponding to it. Then using Object.assign() and spread syntax you can create the final object.






      let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
      result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
      let keys = Object.keys(o).filter(k => k !== 'time');
      keys.forEach(key => {
      r[key] = r[key] || {[key] : };
      r[key][key].push(o[key]);
      });
      return r;
      },{})));
      console.log(result);








      share|improve this answer

































        0














        const data = a.reduce((acc, row) => {
        // Loop over keys of each row.
        Object.keys(row)
        // Filter out the "time" keys.
        .filter((key) => key !== 'time')
        // Collect the values by key, storing them in the accumulator.
        .forEach((key) => {
        if (typeof acc[key] === 'undefined') {
        acc[key] =
        }

        acc[key].push(row[key])
        })

        return acc
        }, {})





        share|improve this answer


























        • I'd recommend that you add some info about what your code is doing.

          – SiddAjmera
          Nov 16 '18 at 14:00


















        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        0














        Here is a solution, if something is not clear, please lemme know:



        const result = {};

        a.forEach(object => {
        // Loop on each entry in the original array
        Object.entries(object).forEach(([key,value]) => {
        // Loop through each entry of one entry of the original array
        // (ex 'T-1', 'T-2')
        if(key !== 'time') {
        // It's a key that I'm interested in, so do logic
        if(!result[key]) {
        // If the key (example 'T-1') haven't been encountered yet,
        // initialize it's entry in the result with an empty array
        result[key] = ;
        }
        result[key].push(value);
        }
        });
        });





        share|improve this answer




























          0














          Here is a solution, if something is not clear, please lemme know:



          const result = {};

          a.forEach(object => {
          // Loop on each entry in the original array
          Object.entries(object).forEach(([key,value]) => {
          // Loop through each entry of one entry of the original array
          // (ex 'T-1', 'T-2')
          if(key !== 'time') {
          // It's a key that I'm interested in, so do logic
          if(!result[key]) {
          // If the key (example 'T-1') haven't been encountered yet,
          // initialize it's entry in the result with an empty array
          result[key] = ;
          }
          result[key].push(value);
          }
          });
          });





          share|improve this answer


























            0












            0








            0







            Here is a solution, if something is not clear, please lemme know:



            const result = {};

            a.forEach(object => {
            // Loop on each entry in the original array
            Object.entries(object).forEach(([key,value]) => {
            // Loop through each entry of one entry of the original array
            // (ex 'T-1', 'T-2')
            if(key !== 'time') {
            // It's a key that I'm interested in, so do logic
            if(!result[key]) {
            // If the key (example 'T-1') haven't been encountered yet,
            // initialize it's entry in the result with an empty array
            result[key] = ;
            }
            result[key].push(value);
            }
            });
            });





            share|improve this answer













            Here is a solution, if something is not clear, please lemme know:



            const result = {};

            a.forEach(object => {
            // Loop on each entry in the original array
            Object.entries(object).forEach(([key,value]) => {
            // Loop through each entry of one entry of the original array
            // (ex 'T-1', 'T-2')
            if(key !== 'time') {
            // It's a key that I'm interested in, so do logic
            if(!result[key]) {
            // If the key (example 'T-1') haven't been encountered yet,
            // initialize it's entry in the result with an empty array
            result[key] = ;
            }
            result[key].push(value);
            }
            });
            });






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 16 '18 at 12:19









            abdullahkadyabdullahkady

            720147




            720147

























                0














                Best use case for using Map instead of Key,Value arrays. Simplifies lot of things.






                share|improve this answer




























                  0














                  Best use case for using Map instead of Key,Value arrays. Simplifies lot of things.






                  share|improve this answer


























                    0












                    0








                    0







                    Best use case for using Map instead of Key,Value arrays. Simplifies lot of things.






                    share|improve this answer













                    Best use case for using Map instead of Key,Value arrays. Simplifies lot of things.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Nov 16 '18 at 12:26









                    rajesh_kwrajesh_kw

                    1,2031314




                    1,2031314























                        0














                        You can use array#reduce with Object.values() to create a hash of your key and add the values corresponding to it. Then using Object.assign() and spread syntax you can create the final object.






                        let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                        result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                        let keys = Object.keys(o).filter(k => k !== 'time');
                        keys.forEach(key => {
                        r[key] = r[key] || {[key] : };
                        r[key][key].push(o[key]);
                        });
                        return r;
                        },{})));
                        console.log(result);








                        share|improve this answer






























                          0














                          You can use array#reduce with Object.values() to create a hash of your key and add the values corresponding to it. Then using Object.assign() and spread syntax you can create the final object.






                          let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                          result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                          let keys = Object.keys(o).filter(k => k !== 'time');
                          keys.forEach(key => {
                          r[key] = r[key] || {[key] : };
                          r[key][key].push(o[key]);
                          });
                          return r;
                          },{})));
                          console.log(result);








                          share|improve this answer




























                            0












                            0








                            0







                            You can use array#reduce with Object.values() to create a hash of your key and add the values corresponding to it. Then using Object.assign() and spread syntax you can create the final object.






                            let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                            result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                            let keys = Object.keys(o).filter(k => k !== 'time');
                            keys.forEach(key => {
                            r[key] = r[key] || {[key] : };
                            r[key][key].push(o[key]);
                            });
                            return r;
                            },{})));
                            console.log(result);








                            share|improve this answer















                            You can use array#reduce with Object.values() to create a hash of your key and add the values corresponding to it. Then using Object.assign() and spread syntax you can create the final object.






                            let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                            result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                            let keys = Object.keys(o).filter(k => k !== 'time');
                            keys.forEach(key => {
                            r[key] = r[key] || {[key] : };
                            r[key][key].push(o[key]);
                            });
                            return r;
                            },{})));
                            console.log(result);








                            let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                            result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                            let keys = Object.keys(o).filter(k => k !== 'time');
                            keys.forEach(key => {
                            r[key] = r[key] || {[key] : };
                            r[key][key].push(o[key]);
                            });
                            return r;
                            },{})));
                            console.log(result);





                            let data = [ {'time' : 1539664755070,'T-1': 23 }, {'time' : 1539665095442,'H-1': 24 }, {'time' : 1539666489560,'T-1': 42 }, {'time' : 1539665095442,'H-1': 27 }, {'time': 1539671682230,'H-1': 40.45,'T-2': 33}, {'time': 1539671682230,'T-2': 30.45,'T-1': 65},{'time': 1539671682230,'T-2': 42.45,'H-1': 11}, {'time': 1539671682230,'T-1': 50.45,'T-2': 85} ],
                            result = Object.assign({}, ...Object.values(data.reduce((r, o) => {
                            let keys = Object.keys(o).filter(k => k !== 'time');
                            keys.forEach(key => {
                            r[key] = r[key] || {[key] : };
                            r[key][key].push(o[key]);
                            });
                            return r;
                            },{})));
                            console.log(result);






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Nov 16 '18 at 12:48

























                            answered Nov 16 '18 at 12:21









                            Hassan ImamHassan Imam

                            12k31532




                            12k31532























                                0














                                const data = a.reduce((acc, row) => {
                                // Loop over keys of each row.
                                Object.keys(row)
                                // Filter out the "time" keys.
                                .filter((key) => key !== 'time')
                                // Collect the values by key, storing them in the accumulator.
                                .forEach((key) => {
                                if (typeof acc[key] === 'undefined') {
                                acc[key] =
                                }

                                acc[key].push(row[key])
                                })

                                return acc
                                }, {})





                                share|improve this answer


























                                • I'd recommend that you add some info about what your code is doing.

                                  – SiddAjmera
                                  Nov 16 '18 at 14:00
















                                0














                                const data = a.reduce((acc, row) => {
                                // Loop over keys of each row.
                                Object.keys(row)
                                // Filter out the "time" keys.
                                .filter((key) => key !== 'time')
                                // Collect the values by key, storing them in the accumulator.
                                .forEach((key) => {
                                if (typeof acc[key] === 'undefined') {
                                acc[key] =
                                }

                                acc[key].push(row[key])
                                })

                                return acc
                                }, {})





                                share|improve this answer


























                                • I'd recommend that you add some info about what your code is doing.

                                  – SiddAjmera
                                  Nov 16 '18 at 14:00














                                0












                                0








                                0







                                const data = a.reduce((acc, row) => {
                                // Loop over keys of each row.
                                Object.keys(row)
                                // Filter out the "time" keys.
                                .filter((key) => key !== 'time')
                                // Collect the values by key, storing them in the accumulator.
                                .forEach((key) => {
                                if (typeof acc[key] === 'undefined') {
                                acc[key] =
                                }

                                acc[key].push(row[key])
                                })

                                return acc
                                }, {})





                                share|improve this answer















                                const data = a.reduce((acc, row) => {
                                // Loop over keys of each row.
                                Object.keys(row)
                                // Filter out the "time" keys.
                                .filter((key) => key !== 'time')
                                // Collect the values by key, storing them in the accumulator.
                                .forEach((key) => {
                                if (typeof acc[key] === 'undefined') {
                                acc[key] =
                                }

                                acc[key].push(row[key])
                                })

                                return acc
                                }, {})






                                share|improve this answer














                                share|improve this answer



                                share|improve this answer








                                edited Nov 16 '18 at 14:04

























                                answered Nov 16 '18 at 12:16









                                Denis FrezzatoDenis Frezzato

                                43029




                                43029













                                • I'd recommend that you add some info about what your code is doing.

                                  – SiddAjmera
                                  Nov 16 '18 at 14:00



















                                • I'd recommend that you add some info about what your code is doing.

                                  – SiddAjmera
                                  Nov 16 '18 at 14:00

















                                I'd recommend that you add some info about what your code is doing.

                                – SiddAjmera
                                Nov 16 '18 at 14:00





                                I'd recommend that you add some info about what your code is doing.

                                – SiddAjmera
                                Nov 16 '18 at 14:00



                                Popular posts from this blog

                                Xamarin.iOS Cant Deploy on Iphone

                                Glorious Revolution

                                Dulmage-Mendelsohn matrix decomposition in Python