Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

217
Visualizações
Two map methods inside map method

In my example, I have an array of objects which I want to map to another array of objects:

Resurs.map((item) => ({
  Cen: item.Cent,
  level: [
    item.NumList.map((item) => ({
      Kom: item.Number,
      Num: item.Kor,
    })),
    item.SerList.map((item) => ({
      Kom2: item.Ser,
    })),
  ],
}));

So, I have 2 map methods inside the map method. It will return:

{
  Cen: "aaa",
  level: [
    [ // -> want to get rid of this
      {
        Kom: "aaa",
        Num: "aaa",
      },
      {
        Kom: "bbb",
        Num: "bbb",
      },
    ], // -> and this
    [ //-> and this
      { Kom2: "aaa" },
      { Kom2: "bbb" },
    ], //-> and this
  ],
};

So, both map functions need to be inside level key, but not as a list which has two lists inside, but as a list with objects.

So, I want to achieve:

{
  Cen: "aaa",
  level: [
    {
      Kom: "aaa",
      Num: "aaa",
    },
    {
      Kom: "bbb",
      Num: "bbb",
    },
    { Kom2: "aaa" },
    { Kom2: "bbb" },
  ],
};
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You are almost there! The answer is spread operator. Just insert it in your inner map.

You can use a spread operator to expanded the arrays when you insert into level.

The spread operator can be used with an array or an object.

Example:

var a = [1, 2, 3, 4];
var b = [5, 6, 7];

var c = [a, b];   //  [[1, 2, 3, 4], [5, 6, 7]]  // This is what you are doing

Solution: 
var d = [...a, ...b];   //  [1, 2, 3, 4, 5, 6, 7]

Complete Solution:

const Resurs = [
    {
        Cent:  "centValue",
        NumList: [
            {
                Number: "numValue1",
                Kor: "KorValue1"
            },
            {
                Number: "numValue3",
                Kor: "KorValue2"
            },
            {
                Number: "numValue3",
                Kor: "KorValue3"
            }
        ],
        SerList: [
            {
                Ser: "SerValue1"
            },
            {
                Ser: "SerValue2"
            }
        ]
    }
];
const data = Resurs.map((item) => ({
  Cen: item.Cent,
  level: [
    ...item.NumList.map((item) => ({   // Just add ...
      Kom: item.Number,
      Num: item.Kor,
    })),
    ...item.SerList.map((item) => ({   // Just add ...
      Kom2: item.Ser,
    })),
  ],
}));

console.log(data);

about 4 years ago · Juan Pablo Isaza Relatório

0

Don't create the outer array in the first place:

const data = Resurs.map((item) => ({
  Cen: item.Cent,
  level: item.NumList.map((item) => ({
      Kom: item.Number,
      Num: item.Kor,
    })).concat(item.SerList.map((item) => ({
      Kom2: item.Ser,
    }))),
}));
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda