1

I have the following object:

const movies = {
  1: {
    id: 1,
    name: 'Planet Earth',
  },
  2: {
    id: 2,
    name: 'Selma',
  },
  3: {
    id: 3,
    name: 'Million Dollar Baby',
  },
  4: {
    id: 4,
    name: 'Forrest Gump',
  },
  5: {
    id: 5,
    name: 'Get Out',
  },
};

Then I want an array with only the property id. To do so I've tried something like:

const moviesArray = Object.values(movies);
const idArray = moviesArray.map(movie => Object.values(movie)[0]);
console.log(idArray);

It prints idArray properly but my question is if am I missing a method to solve this problem.

3
  • 3
    Object.keys(movies) Commented Jan 10, 2019 at 12:24
  • 1
    Object.values(movies).map(movie => movie.id) Commented Jan 10, 2019 at 12:26
  • 3
    is there any reason to store the objects in an object with numbers as properties? Storing it in array would be more appropriate here. Commented Jan 10, 2019 at 12:27

5 Answers 5

4

You could use the id property directly:

const
    movies = { 1: { id: 1, name: 'Planet Earth' }, 2: { id: 2, name: 'Selma' }, 3: { id: 3, name: 'Million Dollar Baby' }, 4: { id: 4, name: 'Forrest Gump' }, 5: { id: 5, name: 'Get Out' } },
    moviesArray = Object.values(movies),
    idArray = moviesArray.map(movie => movie.id);

console.log(idArray);

Sign up to request clarification or add additional context in comments.

2 Comments

Yes I forgot I can simply access to 'movie.id', but is there any method to do this directly instead of values and map?
@Jquino, actually you nee the two steps to access unknown properties in an array and mapping for the property.
1

const movies = {
  1: {
    id: 1,
    name: 'Planet Earth',
  },
  2: {
    id: 2,
    name: 'Selma',
  },
  3: {
    id: 3,
    name: 'Million Dollar Baby',
  },
  4: {
    id: 4,
    name: 'Forrest Gump',
  },
  5: {
    id: 5,
    name: 'Get Out',
  },
};

const moviesArray = Object.values(movies);
const idArray = moviesArray.map(movie => movie.id);
console.log(idArray);

Comments

1

I this case, I'd be more inclined to use movie => movie.id as your mapper function, rather than movie => Object.values(movie)[0].

The issue with your current function is that it assumes id will always happen to be the first property in the Array returned by Object.values. That happens to be true with your current function as written, but I'm not sure you can necessarily guarantee that in the general case. Directly referencing movie.idworks even if the properties come in a different order. It should also be a bit faster, since you don't have to convert eaxh individual object to an Array each time.

Comments

0

I think there wasn't a need for using Object.values in the map part here. It would have been same without it:

const movies = {
  1: {
    id: 1,
    name: 'Planet Earth',
  },
  2: {
    id: 2,
    name: 'Selma',
  },
  3: {
    id: 3,
    name: 'Million Dollar Baby',
  },
  4: {
    id: 4,
    name: 'Forrest Gump',
  },
  5: {
    id: 5,
    name: 'Get Out',
  },
};
const moviesArray = Object.values(movies);
    const idArray = moviesArray.map(movie => movie);
    console.log(moviesArray);

Comments

0

May be you can go with more core version. In my solution the loop will be running only once.

const movies = {
  1: {
    id: 1,
    name: 'Planet Earth',
  },
  2: {
    id: 2,
    name: 'Selma',
  },
  3: {
    id: 3,
    name: 'Million Dollar Baby',
  },
  4: {
    id: 4,
    name: 'Forrest Gump',
  },
  5: {
    id: 5,
    name: 'Get Out',
  },
};


const idArray = [];
for (let i in movies) {
	idArray.push(movies[i].id);
}

console.log(idArray);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.