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

70
Visualizações
How to get value in single array extracted from nested array only through map function

How to get value in single array not in nested array without any additional operation outside map function.

const List = []
List.push(
    {
        name: 'John',
        id: 383,
        Value: [
            {
                "Age": "10"
            },
            {
                "Age": "20"
            }
        ],
    },
    {
        name: 'Mark',
        id: 545,
        Value: [
            {
                "Age": "30"
            },
            {
                "Age": "40"
            }
        ],
    }
)

const ageList = List.map((list, index) => {

    let age = [];
    list.Value.map(item => {
        age.push(item.Age)
    })
    return age
})
console.log(ageList)

Orignal Output : [ [ '10', '20' ], [ '30', '40' ] ]
Expected Output : [10,20,30,40]

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

This should work well The age array needs to be declared outside

var age=[]
const ageList = List.map((list, index) => {

    
    list.Value.map(item => {
        age.push(item.Age)
    })
    return;
})
console.log(age)
about 4 years ago · Juan Pablo Isaza Relatório

0

as I understand your code in your age array you are exactly getting the desired result. instead of printing and assigning agelist you can just use the map methods as below

const List = []

List.push(
{
    name: 'John',
    id: 383,
    Value: [
        {
            "Age": "10"
        },
        {
            "Age": "20"
        }
    ],
},
{
    name: 'Mark',
    id: 545,
    Value: [
        {
            "Age": "30"
        },
        {
            "Age": "40"
        }
    ],
}

)

List.map((list, index) => {

let age = [];
list.Value.map(item => {
    age.push(item.Age)
    
})

}) console.log(age)

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this way :

const List = [{
  name: 'John',
  id: 383,
  Value: [
    {
      "Age": "10"
    },
    {
      "Age": "20"
    }]
},{
  name: 'Mark',
  id: 545,
  Value: [
    {
      "Age": "30"
    },
    {
      "Age": "40"
    }
  ],
}];

const age = [];

List.forEach((list, index) => {
  list.Value.forEach(item => {
    age.push(item.Age);  
  })
});

console.log(age);

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