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

74
Visualizações
adding property boolean value based on array property

I need to get a property added based on the one of the array property. Since already I am looping through the array is there a simple way to achieve below output from the input, Also I don't want to make the input array mutated

Input array

const a = [{is_done: true, name: 'a'}, {is_done: true, name: 'b'}, {is_done: true, name: 'c'}]

Output array

[
    {
        "is_done": true,
        "name": "a",
        "which_is_last_done": false
    },
    {
        "is_done": true,
        "name": "b",
        "which_is_last_done": false
    },
    {
        "is_done": true,
        "name": "c",
        "which_is_last_done": true
    }
]

I am able to achieve this output using the below snippet, is there a better way.

const a = [{
  is_done: true,
  name: 'a'
}, {
  is_done: true,
  name: 'b'
}, {
  is_done: true,
  name: 'c'
}];

const output = a.reduce(
  (acc, item, i, array) => {
    acc.items.push({
      ...item,
      which_is_last_done: [...array].reverse().find(item => item.is_done)?.name === item.name,

    });
    return acc;
  }, {
    items: []
  }
);

console.log(output.items)

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

0

You can first look for the index of the "last done" element. Afterwards copy all objects and for the element with this index set which_is_last_done to true.

const a = [{
  is_done: true,
  name: 'a'
}, {
  is_done: true,
  name: 'b'
}, {
  is_done: true,
  name: 'c'
}];

const lastDoneIndex = a.map(item => item.is_done).lastIndexOf(true)
const output = a.map((item, index) => {
  return {
    ...item,
    which_is_last_done: index === lastDoneIndex,
  }
})

console.log(output)

about 4 years ago · Juan Pablo Isaza Relatório

0

const a = [{
  is_done: true,
  name: 'a'
}, {
  is_done: true,
  name: 'b'
}, {
  is_done: true,
  name: 'c'
}];

// make deep copy of input
const output = a.map(el => ({...el, which_is_done_last: false}));
// find last element that's done
const last_done = [...output].reverse().find(({is_done}) => is_done);
if (last_done) {
  // update boolean property
  last_done.which_is_done_last = true;
}

console.log(output)

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