简体   繁体   English

将JavaScript对象属性设置为数组

[英]JavaScript objects property to array

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. 然后我想要一个仅具有属性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. 它可以正确打印idArray ,但是我的问题是我是否缺少解决此问题的方法。

You could use the id property directly: 您可以直接使用id属性:

 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); 

 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); 

I this case, I'd be more inclined to use movie => movie.id as your mapper function, rather than movie => Object.values(movie)[0] . 在这种情况下,我更倾向于使用movie => movie.id作为您的映射器函数,而不是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 . 当前函数的问题在于,它假定id总是碰巧是Object.values返回的Array中的第一个属性。 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.id works even if the properties come in a different order. 即使属性的顺序不同,直接引用movie.id可以。 It should also be a bit faster, since you don't have to convert eaxh individual object to an Array each time. 它也应该更快一些,因为您不必每次都将eaxh单个对象转换为Array。

I think there wasn't a need for using Object.values in the map part here. 我认为这里不需要在map部分中使用Object.values 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); 

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); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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