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

135
Visualizações
How to modify an array of objects if two following objects have different value?

Consider this array:

const arr = [
  {order: 1, mess:"test1"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},
  {order: 3, mess:"test3"},
  {order: 4, mess:"test4"},
]

arr is always sorted by order.

I want to modify it to get:

const arr = [
  {order: 1, mess:"test1"},

  {order: 2, mess:"test2", sep:true},
  {order: 2, mess:"test2"},
  {order: 2, mess:"test2"},

  {order: 3, mess:"test3", sep:true},

  {order: 4, mess:"test4", sep:true},
]

sep must be inserted into an object if it has an order different from the preceding object.

I'm unable to write this function, even if it might be quite simple (I guess!).
At this time, I have this, but it doesn't work at all and I don't understand its behavior.

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]


arr.forEach(function(value, i) {
  i = Number(i)
  const y = i + 1
  if (arr[i] !== undefined && arr[y] !== undefined) {
    if (arr[i].order === arr[y].order) arr[y].sep = true;
  }
});

console.log(arr)

Do you have any idea? Thanks

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

0

You could map a new array and check the item and the predecessor.

const
    data = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }],
    result = data.map((o, i, a) => i && a[i - 1].order !== o.order
        ? { ...o, sep: true }
        : o
    );

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Try this

const arr = [
  {order: 1, mess: "test1"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 2, mess: "test2"},
  {order: 3, mess: "test3"},
  {order: 4, mess: "test4"},
  ]

arr.forEach((obj,index)=>{
  if(index>0 && obj.order != arr[index-1]['order']){
    arr[index]['sep']=true
  }
});
console.log(arr)

about 4 years ago · Juan Pablo Isaza Relatório

0

Simplest change of existing array

const arr = [{ order: 1, mess: "test1" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 2, mess: "test2" }, { order: 3, mess: "test3" }, { order: 4, mess: "test4" }];

arr.forEach((value, i) => {
  if (i && arr[i-1].order !== arr[i].order) arr[i].sep = true;
});
console.log(arr)

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