Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!