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

106
Visualizações
How to mutate an object in a array if it has the same key using javascript

Kindly help me to mutate the object in javascript. Lets say I have a below list of object, I want to objects if the keys are same in the first list Key "FOREFOOT - OUTER" repeated twice, value of this key different, in the first occurrence left is true and in the second occurrence right is true, I want this to be merges as "forefoot - outer" : {left: true, right: true}

[
    {
        "FOREFOOT - OUTER": {
            "left": true,
            "right": false
        }
    },
    {
        "FOREFOOT": {
            "left": false,
            "right": true
        }
    },
    {
        "FOREFOOT - INNER": {
            "left": false,
            "right": true
        }
    },
    {
        "FOREFOOT - OUTER": {
            "left": false,
            "right": true
        }
    },     
]

Expected answer is

[
    {
        "FOREFOOT": {
            "left": false,
            "right": true
        }
    },
    {
        "FOREFOOT - INNER": {
            "left": false,
            "right": true
        }
    },
    {
        "FOREFOOT - OUTER": {
            "left": true,
            "right": true
        }
    },
   
]

Can anyone please help me to find a solution for this in javascript ES6.

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

0

You could do a little function like this :

const filterArray = (arrayToFilter) => {
    const newArray = arrayToFilter;
    let i = -1;
    while (++i < newArray.length - 1) {
        // So we brows the array since the start finally.
        j = i;
        while (++j < newArray.length) {
            // We create a string to could use it in our if
            const item_i = Object.keys(newArray[i]) + '';
            const item_j = Object.keys(newArray[j]) + '';

            // If the 2nd occure is true, then we put the 1st true
            if (item_i == item_j) {
                if (newArray[j][item_j].left)
                    newArray[i][item_i].left = true;
                if (newArray[j][item_j].right)
                    newArray[i][item_i].right = true;
                newArray.splice(j, 1);
                // Seeing that we delete an element, we decrease our J
                j--;
            }
        }
    }
    return newArray;
}

So this is in order to merges only "true" value. Because in your sample you merges only True value. If you want merges false too, edit the if with :

if (item_i == item_j) {
    newArray[i][item_i].left = newArray[j][item_j].left;
    newArray[i][item_i].right = newArray[j][item_j].right;
    newArray.splice(j, 1);
    j--;
}
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