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

132
Visualizações
Extract value from array of objects

So I have this array of objects:

[ { type: 'month', value: '9' },
  { type: 'day', value: '11' },
  { type: 'year', value: '2021' },
  { type: 'hour', value: '7' },
  { type: 'minute', value: '35' },
  { type: 'second', value: '07' }  ]

I need a way to extract the value 9 using the search term month. Sure I could use:

var myObjects = [ 
  { type: 'month', value: '9' },
  { type: 'day', value: '11' },
  { type: 'year', value: '2021' },
  { type: 'hour', value: '7' },
  { type: 'minute', value: '35' },
  { type: 'second', value: '07' }  
] ;

console.log(myObjects[0]["value"]);

Problem is , this is really not using a search term, and I'm in a situation where the date format can change from en_GB to en_US and other complicated time formats which will make the month switch position to [1], [2] or [3].

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

0

Using Array#find:

const data = [ { type: 'month', value: '9' }, { type: 'day', value: '11' }, { type: 'year', value: '2021' }, { type: 'hour', value: '7' }, { type: 'minute', value: '35' }, { type: 'second', value: '07' } ];

const { value } = data.find(({ type }) => type === 'month') || {};

console.log(value);

about 4 years ago · Juan Pablo Isaza Relatório

0

And probably it will be convenient to convert the array of objects into a single object. It can be done this way:

var arr = [
    { type: 'month',  value: '9'   },
    { type: 'day',    value: '11'  },
    { type: 'year',   value: '2021'},
    { type: 'hour',   value: '7'   },
    { type: 'minute', value: '35'  },
    { type: 'second', value: '07'  }
]

const arr_to_obj = arr => {
    var obj = {};
    for (var a of arr) obj[a.type] = a.value;
    return obj;
}

var date = arr_to_obj(arr); // { month:9, day:11, year:2021, ... }

console.log(date.month); // 9
console.log(date.day);   // 11
console.log(date.year);  // 2021
console.log(date.hour);  // 7     ...etc

about 4 years ago · Juan Pablo Isaza Relatório

0

One more variant:

var arr = [{ type: 'month', value: '9' },
  { type: 'day', value: '11' },
  { type: 'year', value: '2021' },
  { type: 'hour', value: '7' },
  { type: 'minute', value: '35' },
  { type: 'second', value: '07' }];

// var month = arr.filter(x => x.type == 'month')[0].value; // less efficient

var month = arr.find(x => x.type == 'month').value; // more efficient

console.log(month) // 9

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