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

258
Visualizações
Best way to change a value of a property. It's within an object of an array of objects

I'm trying to switch the value of selected from whatever it currently is (true or false) to the opposite when a user clicks on a checkbox. My toggleCheck function is supposed to return all data with the altered select value. Here's the data structure. It's an object with arrays that have objects.

{
    "ModuleName": [{
            "module": "string",
            "selected": false,
            "name": "title",
            "points": [{
                    "category": "category1",
                    "description": "desc1",
                    "provided": "provided1"
                },
                {
                    "category": "category1",
                    "description": "desc",
                    "provided": "something"
                }
            ]
        },
        {
            "module": "string",
            "selected": false,
            "name": "title_one",
            "points": [{
                    "category": "category1",
                    "description": "desc1",
                    "provided": "provided1"
                },
                {
                    "category": "category1",
                    "description": "desc",
                    "provided": "something"
                }
            ]
        }
    ],
    [...],
    [...]
}

My closest attempt but it's the wrong format

function toggleCheck(checkedValue, valuesArr) {
    return {
        value: Object.entries(valuesArr).map(([key, values], i) =>
            values.map((x) => {
                return {
                    ...x,
                    selected:
                        x.name === checkedValue.name ? !x.selected : x.selected,
                };
            })
        ),
    };
}

probably cause I used a map it wrapped it in an array

[
   [ 
      {},
      {},
      {}
   ],
   [...],
   [...]
]

^^^ What I got

{
    "Name": [  {},
               {},
               {}
            ],
    "Name": [...],
    "Name": [...]
}

^^^ What I need

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

0

function toggleCheck(checkedValue, valuesArr) {

    //Step 0: Change the value in the data
    let checked = Object.entries(valuesArr).map(([key, values], i) =>
        values.map((x) => {
            return {
                ...x,
                selected:
                    x.name === checkedValue.name ? !x.selected : x.selected,
            };
        })
    );

    //Step 1: Group the modules into like arrays
    let grouped = checked .flat().reduce(function (r, a, i) {
        if (!i || r[r.length - 1][0].module !== a.module) {
            return r.concat([[a]]);
        }
        r[r.length - 1].push(a);
        return r;
    }, []);

    //Step 2: Assign the new object properties as the arrays. 
    //The key being the module name.
    let newObj = {};
    grouped.map((x, i) => {
        newObj[x[0].module] = x;
    });

    return {value: newObj};
}
about 4 years ago · Juan Pablo Isaza Relatório

0

In your question you wrote:

{
    "Name": [  {},
               {},
               {}
            ],
    "Name": [...],
    "Name": [...]
}

^^^ What I need


this can't be exist in JS object. Only the last assignment of the name property will be taken into account, all the previous ones are only old values of this property

codding :

const data = 
  { name:'aaa'
  , name:'bbb'
  , name:'ccc' 
  }

is the same as coding

const data = {}
data.name = 'aaa'
data.name = 'bbb'
data.name = 'ccc'

proof:

const data = 
  { name:'aaa'
  , name:'bbb'
  , name:'ccc' 
  }

console.log( data )
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}

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