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

182
Visualizações
Empalme eliminando los primeros elementos de la matriz de objetos en lugar del índice

Estoy tratando de excluir elementos de una matriz de objetos en función de una matriz con los índices que tengo que eliminar, pero en cambio, estoy eliminando los primeros elementos de la matriz de objetos.

Mi código:

 let historicPrecipitation = [{"indicator":"Historic Precipitation","month":"1","year":"2014","value":"228.5"},{"indicator":"Historic Precipitation","month":"2","year":"2014","value":"144.7"},{"indicator":"Historic Precipitation","month":"3","year":"2014","value":"120.3"},{"indicator":"Historic Precipitation","month":"4","year":"2014","value":"146.9"},{"indicator":"Historic Precipitation","month":"5","year":"2014","value":"146.4"},{"indicator":"Historic Precipitation","month":"6","year":"2014","value":"128.1"},{"indicator":"Historic Precipitation","month":"7","year":"2014","value":"139.2"},{"indicator":"Historic Precipitation","month":"8","year":"2014","value":"150.0"},{"indicator":"Historic Precipitation","month":"9","year":"2014","value":"199.7"},{"indicator":"Historic Precipitation","month":"10","year":"2014","value":"268.4"},{"indicator":"Historic Precipitation","month":"11","year":"2014","value":"98.5"},{"indicator":"Historic Precipitation","month":"12","year":"2014","value":"139.9"}]; let monthsWithoutRegistration = [4, 5]; for (let i = 0; i < monthsWithoutRegistration.length; i++) { for (let j = 0; j < historicPrecipitation.length; j++) { if (parseInt(historicPrecipitation[j]['month']) === parseInt(monthsWithoutRegistration[i])) { historicPrecipitation.splice(historicPrecipitation[j], 1); } } } console.log(historicPrecipitation);

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

0

Necesita usar j en lugar de historicPrecipitation[j] en el splice porque debe pasar index , no el object :

 let historicPrecipitation = [{ "indicator": "Historic Precipitation", "month": "1", "year": "2014", "value": "228.5" }, { "indicator": "Historic Precipitation", "month": "2", "year": "2014", "value": "144.7" }, { "indicator": "Historic Precipitation", "month": "3", "year": "2014", "value": "120.3" }, { "indicator": "Historic Precipitation", "month": "4", "year": "2014", "value": "146.9" }, { "indicator": "Historic Precipitation", "month": "5", "year": "2014", "value": "146.4" }, { "indicator": "Historic Precipitation", "month": "6", "year": "2014", "value": "128.1" }, { "indicator": "Historic Precipitation", "month": "7", "year": "2014", "value": "139.2" }, { "indicator": "Historic Precipitation", "month": "8", "year": "2014", "value": "150.0" }, { "indicator": "Historic Precipitation", "month": "9", "year": "2014", "value": "199.7" }, { "indicator": "Historic Precipitation", "month": "10", "year": "2014", "value": "268.4" }, { "indicator": "Historic Precipitation", "month": "11", "year": "2014", "value": "98.5" }, { "indicator": "Historic Precipitation", "month": "12", "year": "2014", "value": "139.9" }]; let monthsWithoutRegistration = [4, 5]; for (let i = 0; i < monthsWithoutRegistration.length; i++) { for (let j = 0; j < historicPrecipitation.length; j++) { if (parseInt(historicPrecipitation[j]['month']) === parseInt(monthsWithoutRegistration[i])) { historicPrecipitation.splice(j, 1); } } } console.log(historicPrecipitation);

about 4 years ago · Juan Pablo Isaza Relatório

0

La sintaxis de empalme es incorrecta,

reemplazar con esto

 historicPrecipitation.splice(j, 1);
about 4 years ago · Juan Pablo Isaza Relatório

0

Puede utilizar el filter en lugar de los bucles for anidados:

 let filteredHistoricPrecipitation = historicPrecipitation.filter(x => !monthsWithoutRegistration.includes(parseInt(x['month'])) );
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