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

96
Visualizações
How can I ceate new array of elements based on a pattern found in another array?

I have an array that contains a bunch of random elements such as:

[{
    "id":332,
    "foo":6,
    "event":"ABC-CNEG Correlations Ed3.0 Update 9 (XY-AHZ)",
    "date":"2018-11-26T00:00:00.000Z",
 },
 {
    "id":324,
    "foo":1,
    "event":"ABC 3166-1_2013 No. VII-11",
    "date":"2018-06-30T00:00:00.000Z",
 },
 {
    "id":325,
    "foo":1,
    "event":"ABC 3166-2_2013 To. XII-32",
    "date":"2018-06-30T00:00:00.000Z",

 },
 {
    "id":326,
    "foo":2,
    "event":"ABC 3166-1_2016 No. VII-12",
    "date":"2018-06-30T00:00:00.000Z",
 },
 {
    "id":327,
    "foo":1,
    "event":"ABC 3166-2_2018 To. XII-31",
    "date":"2018-06-30T00:00:00.000Z",

 }]

What I need to do is "cherry pick" the above items and create a new array that contains ONLY the items that contain the following string: 3166-1. So, the expected output would be:

[
 {
    "id":324,
    "foo":1,
    "event":"ABC 3166-1_2013 No. VII-11",
    "date":"2018-06-30T00:00:00.000Z",
 },
 {
    "id":326,
    "foo":2,
    "event":"ABC 3166-1_2016 No. VII-12",
    "date":"2018-06-30T00:00:00.000Z",
 }
]

My use case for this is so I can provide a filtered set of options for an HTML drop down select input.

Does anyone have any idea on how I can satisfy my use case and get a filtered set of items in a new array?

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

0

You could use filter function of the array and includes method of strings :

let arr=[{
    "id":332,
    "foo":6,
    "event":"ABC-CNEG Correlations Ed3.0 Update 9 (XY-AHZ)",
    "date":"2018-11-26T00:00:00.000Z",
 },
 {
    "id":324,
    "foo":1,
    "event":"ABC 3166-1_2013 No. VII-11",
    "date":"2018-06-30T00:00:00.000Z",
 },
 {
    "id":325,
    "foo":1,
    "event":"ABC 3166-2_2013 To. XII-32",
    "date":"2018-06-30T00:00:00.000Z",

 },
 {
    "id":326,
    "foo":2,
    "event":"ABC 3166-1_2016 No. VII-12",
    "date":"2018-06-30T00:00:00.000Z",
 },
 {
    "id":327,
    "foo":1,
    "event":"ABC 3166-2_2018 To. XII-31",
    "date":"2018-06-30T00:00:00.000Z",

 }]
 
 
 let result=arr.filter(item=>item.event.includes('3166-1'))
 
 console.table(result)

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