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

263
Visualizações
How do I delete words that start with the keyword in the object value? javascript

I have an array like this. I want to delete row-? words in className property.

[
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3 row-1"
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6 row-8"
  }
]

I want a result like this.

[
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3"
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6"
  }
]

How can I do that ? Also how can I trim so that there is no space at the end?

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

0

1) You can map over the array and then split the className, filter it and then join it.

const arr = [
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3 row-1",
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6 row-8",
  },
];

const result = arr.map((o) => ({
  ...o,
  className: o.className
    .split(" ")
    .filter((s) => !s.startsWith("row"))
    .join(" "),
}));

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

2) You can also use regex /\s*row-\d+/g

enter image description here

const arr = [{
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3 row-1",
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "row-12 form-control row-6 col-lg-6 row-8",
  },
];

const result = arr.map((o) => ({
  ...o,
  className: o.className.replace(/\s*row-\d+/g, "").trim(),
}));

console.log(result);
/* This is not a part of answer. It is just to give the output fill height. So IGNORE IT */
.as-console-wrapper { max-height: 100% !important; top: 0; }

or /\s*row-[^\s]+\s*/g

enter image description here

const result = arr.map((o) => ({
  ...o,
  className: o.className.replace(/\s*row-[^\s]+\s*/g, " ").trim(),
}));
about 4 years ago · Juan Pablo Isaza Relatório

0

const data =[
  {
    type: "text",
    name: "text-1632646960432-0",
    satir: "1",
    className: "form-control col-lg-3 row-1"
  },
  {
    type: "text",
    name: "text-1632646974512-0",
    satir: "1",
    className: "form-control col-lg-6 row-8"
  }
];

const results = data.map((item) => {
  if (item.className.includes('row')) {
    return {
      ...item,
      className: item.className.replace(/row-[0-9]/, '').trim()
    }
  }
});

console.log(results);

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