-2

This is the original Array: It can have one or more objects. In each object there is an adress and an array of mitglieder.

    const original = [
    {
      "adresse":{
        "strasse":"Streetone",
        "plz":"55555",
        "ort":"Hamburg",
        "xy": "aaaaaa"
      },
      "mitglieder":[
        {
          "zugestimmt":true,
          "nummer":"345",
          "nachname":"Meier",
          "vorname":"Peter",
          "geburtsDatum":"1980-01-01"
        },
        {
          "zugestimmt":true,
          "nummer":"435",
          "nachname":"Haushalt",
          "vorname":"Anderer",
          "geburtsDatum":"1980-01-01"
        },
      ]
    },
    {
      "adresse":{
        "street":"Streettwo",
        "plz":"34444",
        "ort":"Hamburg",
        "xy": "bbbbb"
      },
      "mitglieder":[
        {
          "zugestimmt":true,
          "nummer":"345",
          "nachname":"Muster",
          "vorname":"Maria",
          "geburtsDatum":"1980-01-01",
          "xy" : {a:'1', b:'2'}
        },
        {
          "zugestimmt":true,
          "nummer":"345",
          "nachname":"Muster",
          "vorname":"Maria",
          "geburtsDatum":"1980-01-01",
          "xy" : {a:'1', b:'2'}
        },
      ]
    }
  ];

And I want to map it into this Array: This should be an array of the mitglieder of all the objects from the original array. The street, plz and ort ist from the adress key. The "arrayIwant" should only have these properties not the property"xy" or other that can be in the original array.

let arrayIwant = [
    {
    vorname: original[0].mitglieder[0].vorname,
    nachname: original[0].mitglieder[0].nachname,
    geburtsdatum: original[0].mitglieder[0].geburtsDatum,
    zugestimmt: original[0].mitglieder[0].zugestimmt,
    strasse: original[0].adressse.street,
    plz: original[0].adresse.plz,
    ort: original[0].adresse.ort,
    
  },
  {
     vorname: original[0].mitglieder[1].vorname,
    nachname: original[0].mitglieder[1].nachname,
    geburtsdatum: original[0].mitglieder[1].geburtsDatum,
    zugestimmt: original[0].mitglieder[1].zugestimmt,
    strasse: original[0].adresse.street,
    plz: original[0].adresse.plz,
    ort: original[0].adresse.ort,
  },
  {
    vorname: original[1].mitglieder[0].vorname,
    nachname: original[1].mitglieder[0].nachname,
    geburtsdatum: original[1].mitglieder[0].geburtsDatum,
    zugestimmt: original[1].mitglieder[0].zugestimmt,
    strasse: original[1].adresse.street,
    plz: original[1].adresse.plz,
    ort: original[1].adresse.ort,
  },
  {
    vorname: original[1].mitglieder[1].vorname,
    nachname: original[1].mitglieder[1].nachname,
    geburtsdatum: original[1].mitglieder[1].geburtsDatum,
    zugestimmt: original[1].mitglieder[1].zugestimmt,
    strasse: original[1].adresse.street,
    plz: original[1].adresse.plz,
    ort: original[1].adresse.ort,
  }
 ]

thanks for your help

1
  • I’m voting to close this question because the author showed no attempts at a solution, and thus the question reads like a homework question. Commented Sep 19, 2022 at 22:38

2 Answers 2

2

Use Array#flatMap and Array#map with the spread operator as follows:

const 
    original = [ { "adresse":{ "street":"Streetone", "plz":"55555", "ort":"Hamburg" }, "mitglieder":[ { "zugestimmt":true, "nummer":"345", "nachname":"Meier", "vorname":"Peter", "geburtsDatum":"1980-01-01" }, { "zugestimmt":true, "nummer":"435", "nachname":"Haushalt", "vorname":"Anderer", "geburtsDatum":"1980-01-01" }, ] }, { "adresse":{ "street":"Streettwo", "plz":"34444", "ort":"Hamburg" }, "mitglieder":[ { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01" }, { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01" }, ] } ],

    output = original.flatMap(
        ({adresse,mitglieder}) => 
            mitglieder.map(mitg => delete mitg.nummer && ({...mitg,...adresse}))
    );

    console.log( output );

Alternatively .... .. .

You may have to list all the desired sub-properties from the two properties as show in this demo. In the process you can alias any property whose name you would like to change for example to change street to strasse alias like so: street:strasse then use strasse in the final object as shown.

const 
    original = [ { "adresse":{ "street":"Streetone", "plz":"55555", "ort":"Hamburg", "xy": "aaaaaa" }, "mitglieder":[ { "zugestimmt":true, "nummer":"345", "nachname":"Meier", "vorname":"Peter", "geburtsDatum":"1980-01-01" }, { "zugestimmt":true, "nummer":"435", "nachname":"Haushalt", "vorname":"Anderer", "geburtsDatum":"1980-01-01" }, ] }, { "adresse":{ "street":"Streettwo", "plz":"34444", "ort":"Hamburg", "xy": "bbbbb" }, "mitglieder":[ { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01", "xy" : {a:'1', b:'2'} }, { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01", "xy" : {a:'1', b:'2'} }, ] } ],

    output = original.flatMap(
        ({
            adresse: {street: strasse,plz,ort},
            mitglieder //still an array of objects, so wait ...
        }) =>
        mitglieder.map(
            //now you have an object, so you can list the desired props:
            ({vorname,nachname,geburtsDatum,zugestimmt}) =>
            ({vorname,nachname,geburtsDatum,zugestimmt,strasse,plz,ort})
        )
    );

    console.log( output );

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks PeterKA, but the property "xy" or others that are not in the "arrayIwant" should not be in the "arrayIwant".
You can either delete as shown or just list the properties you want. Looks like you changed the question once answers were provided. You may want to show us what you have done so far before we can update our answers. Give it a try; you now at least have a clue.
Thanks PeterKa, i have done a map on the Output Array to get only the props i need, see below.
1

This is straightforward. You want to create a new object for every element of the mitglieder array in every element of your original array.

 let original = [
   { "adresse":{ "street":"Streetone", "plz":"55555", "ort":"Hamburg", "xy": "aaaaaa" }
   , "mitglieder":
     [ { "zugestimmt":true, "nummer":"345", "nachname":"Meier", "vorname":"Peter", "geburtsDatum":"1980-01-01" }
     , { "zugestimmt":true, "nummer":"435", "nachname":"Haushalt", "vorname":"Anderer","geburtsDatum":"1980-01-01" },
     ]
   },
   { "adresse":{ "street":"Streettwo", "plz":"34444", "ort":"Hamburg", "xy": "aaaaab"}
   , "mitglieder":
     [ { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01" }
     , { "zugestimmt":true, "nummer":"345", "nachname":"Muster", "vorname":"Maria", "geburtsDatum":"1980-01-01" },
     ]
   }
]

let adressePropMap = { 'street': 'strasse', 'plz': 'plz', 'ort': 'ort' }
myArray = original.reduce( (accum, elem) => {
  elem.mitglieder.forEach( (mitglieder) => {
    obj = {}                         // create the new object
    for (const[from, to] of Object.entries(adressePropMap )) {
      obj[to] = elem.adresse[from]
    }
    Object.assign(obj, mitglieder)   // copy the mitglieder properties
    delete obj.nummer                // remove unwanted property
    accum.push(obj)                  // put the new object into the accumulator
  })
  return accum
}, [])

console.log(myArray)

4 Comments

Thanks, its nearly what i wanted but not exactly. The original Array can have properties, that the "arrayIwant" should not have. I have edit the original Array above, with the props 'xy'. These props or other props that can be in the original array should not be in the ""arrayIwant". stackoverflow.com/users/2487517/tibrogargan
If what you're asking for is not what you want, please don't complain that you didn't get what you wanted. You got what you asked for.
Sorry my fault, should i open another question and mark this as solve, what do you suggest?
I've updated it so it maps only specific properties for adresse, but yeah - in future try and not move the goal posts mid-game. You might want to consider using a similar technique for copying the properties from mitglieder. Copying everything and removing one is fairly trivial, but it could start presenting design/maintenance issues if you want to modify multiple fields

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.