-2

I have the following array with 4 Objects:

var btnObj = [
    {sect_title: 'Navigation', btn_title: 'Übersicht'},
    {sect_title: 'Navigation', btn_title: 'Inhaltsverzeichnis'},
    {sect_title: 'Modul 1', btn_title: 'Lehrwerk'},
    {sect_title: 'Modul 1', btn_title: 'Prinzipien'},
]

The 2 distinct section-titles shall each have their 2 btn-titles inside them resulting in:

var btnObjResult = [
    "Navigation" = [
        {btn_title: 'Übersicht'},
        {btn_title: 'Inhaltsverzeichnis'},

    ],
    "Modul 1" = [
        {btn_title: 'Lehrwerk'},
        {btn_title: 'Prinzipien'},
    ]
]

I just cant get my head around how to accomplish this in JS. Thanks a lot for any hint.

MaxWidth
  • 468
  • 3
  • 9
  • 3
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific roadblock you're running into a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Nov 26 '18 at 11:02
  • 1
    and how would you accomplish it in 'notJS' language? – Adam Orłowski Nov 26 '18 at 11:06
  • 1
    Your output object is not valid, do you mean to return an object. Also share the code which you have tried. – Hassan Imam Nov 26 '18 at 11:07
  • Possible duplicate of [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/questions/11922383/access-process-nested-objects-arrays-or-json) – Liam Nov 26 '18 at 11:08

2 Answers2

1

Am guessing this is what you are looking for :

    var btnObj = [
        {sect_title: 'Navigation', btn_title: 'Übersicht'},
        {sect_title: 'Navigation', btn_title: 'Inhaltsverzeichnis'},
        {sect_title: 'Modul 1', btn_title: 'Lehrwerk'},
        {sect_title: 'Modul 1', btn_title: 'Prinzipien'},
    ]

    const transformedObj = btnObj.reduce((obj, {sect_title, ...rest}) => {
        if(!(sect_title in obj)) obj[sect_title] = [];
        obj[sect_title].push(rest);
        return obj;
    },  {});
    
    console.log(transformedObj);
Easwar
  • 5,132
  • 1
  • 11
  • 21
  • that doesn't look like the OPs "expected output" – Liam Nov 26 '18 at 11:11
  • @Liam : Forgot to add the default reduce parameter. Assume its good now. Also the OPs expected output is not a valid object as '=' operator is used. Am assuming that an object was the intention. – Easwar Nov 26 '18 at 11:16
  • the point is the OPs "expected result" is impossible. It's not valid, so you cannot produce what they want. – Liam Nov 26 '18 at 11:18
  • @Liam : Just trying to help out here. Am quite sure, that it was a misspelt typo which made me assume the intention of the OP. But certainly I feel it doesn't warrant a downvote for my answer. – Easwar Nov 26 '18 at 11:25
  • Ok, If I wanted to answer this question, first thing I'd say is "your Javascript is not valid because you can't have a property of a an array" then explain that the OP most likely wants an object as that would make sense, etc, etc then say this is how I'd do that. See where I'm going here? "Am guessing this is what you are looking for" isn't exactly descriptive. I mean at least your replying to comments and made some attempt to explain, [unlike others](https://stackoverflow.com/a/53479864/542251) but this still needs work. – Liam Nov 26 '18 at 11:51
1

    let btnObj = [
        {sect_title: 'Navigation', btn_title: 'Übersicht'},
        {sect_title: 'Navigation', btn_title: 'Inhaltsverzeichnis'},
        {sect_title: 'Modul 1', btn_title: 'Lehrwerk'},
        {sect_title: 'Modul 1', btn_title: 'Prinzipien'},
    ]
    let myData = {}; //if you need in array you can use myData = [];
    btnObj.forEach((d)=>{
     if(!myData[d.sect_title]) myData[d.sect_title] = [];
     myData[d.sect_title].push({btn_title:d.btn_title})
    })
    console.log(myData);
Arshpreet Wadehra
  • 993
  • 2
  • 9
  • 23