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

89
Visualizações
Extract object with same key from multiple array and make them into one array

I have an array of widgets containing multiple objects. Each object contains an array called cards which contains just a single object. I need help with extracting all the cards object and making a new cards array containing all the cards as elements

{
    "widgets" : [
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        },
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        }
    ]
}

How I want is

{
    "widgets": [
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                },
                {
                    "data": {},
                    "layout": 1
                }
            ]
        }
    ]
}
about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You can loop over the original object orig and extract it's cards into a new array allCards:

const allCards = [];
for (let widget of orig.widgets) {
    allCards.push(widget.cards[0]); 
}

Then you can construct a new object with these extracted cards.

about 4 years ago · Juan Pablo Isaza Relatório

0

Given your requirement, the below might work.

let x = {
    "widgets" : [
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        },
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        }
    ]
}
let y = {"widgets": [{...x.widgets[0], "cards":[]}]};
x.widgets.forEach(el => y.widgets[0].cards.push(...[].concat(el.cards)));
about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this code that taek into account the type of each widget :

object = {
    "widgets" : [
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        },
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        },
        {
            "type": "Table",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        },
        {
            "type": "Grid",
            "cards": [
                {
                    "data": {},
                    "layout": 1
                }
            ]
        }
    ]
}

new_object = {"widgets" : []}

for (let i=0;  i < object["widgets"].length; i++) {
    exist = false
    for (let j=0;  j < new_object["widgets"].length; j++) {
        if (new_object["widgets"][j]["type"] === object["widgets"][i]["type"]){
            exist = true
            new_object["widgets"][j]["cards"].push(object["widgets"][i]["cards"][0])
        }
    }
    if (exist === false){
        new_object["widgets"].push(object["widgets"][i])
    }
  
}
console.log(new_object)

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