简体   繁体   中英

accessing specific values of objects nested inside an array

I am trying to access certain key value for objects nested inside an array. When I try to do that with filter(x => x.key('duration'));

I get .key is not a function.

Here I want to access the values of keys "duration" within the objects of the array:

'use strict';

const monday = [
  {
    name: 'Write a summary HTML/CSS',
    duration: 180,
  },
  {
    name: 'Some web development',
    duration: 120,
  },
  {
    name: 'Fix homework for class10',
    duration: 20,
  },
  {
    name: 'Talk to a lot of people',
    duration: 200,
  },
];

const tuesday = [
  {
    name: 'Keep writing summary',
    duration: 240,
  },
  {
    name: 'Some more web development',
    duration: 180,
  },
  {
    name: 'Staring out the window',
    duration: 10,
  },
  {
    name: 'Talk to a lot of people',
    duration: 200,
  },
  {
    name: 'Look at application assignments new students',
    duration: 40,
  },
];

const maartjesTasks = monday.concat(tuesday);
const maartjesHourlyRate = 20;
console.log(maartjesTasks);

```js

To get the list of all duration, do not use Array.filter that will create a new array containing elements matching a specific condition, but Array.map that will create a new array containing the returned values. Here we are returning the key duration, so it creates a new array containing all duration.

Also, when using Array.filter or Array.map , the first argument of the function is the element of the array you are treating.

This element being an object, as example :

{
    name: 'Write a summary HTML/CSS',
    duration: 180,
},

The proper way to access the duration value is to use . notation, like : x.duration .

There is no key method to access an object key.

Also in my example you will see that I use of ... which is called a spread operator . I use it to concatenate both arrays. It's the same as calling concat except that it's a lot faster.

 const monday = [ { name: 'Write a summary HTML/CSS', duration: 180, }, { name: 'Some web development', duration: 120, }, { name: 'Fix homework for class10', duration: 20, }, { name: 'Talk to a lot of people', duration: 200, }, ]; const tuesday = [ { name: 'Keep writing summary', duration: 240, }, { name: 'Some more web development', duration: 180, }, { name: 'Staring out the window', duration: 10, }, { name: 'Talk to a lot of people', duration: 200, }, { name: 'Look at application assignments new students', duration: 40, }, ]; const durations = [ ...tuesday, ...monday, ].map(x => x.duration); console.log(durations); 


NOTA : This do not handle the duplicates duration. If you wants no duplicates, I advise you to take a look at : Set

Try calling the object property directly with a "." like so:

let arr = monday.filter(obj => {
    console.log(obj.duration) // 180, 120, 20, 200 
})

x.key('duration') would call the "key" method of x , which doesn't have one.

Use bracket notation :

filter(x => x['duration']);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM