将JavaScript对象属性设置为数组

时间:2019-01-10 12:22:49

标签: javascript arrays javascript-objects

我有以下对象:

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',
  },
};

然后我想要一个仅包含属性ID的数组。为此,我尝试过类似的操作:

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

它可以正确打印idArray,但是我的问题是我是否缺少解决此问题的方法。

5 个答案:

答案 0 :(得分:4)

您可以直接使用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);

答案 1 :(得分: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);

答案 2 :(得分:1)

在这种情况下,我更倾向于使用async function subscribe() { const response = await fetch('./VapidPublicKey'); const vapidPublicKey = await response.text(); console.log(vapidPublicKey); const convertedVapidKey = urlB64ToUint8Array(vapidPublicKey); var browserOptions={}; if(typeof InstallTrigger !== 'undefined'){ browserOptions={userVisibleOnly: true}; }else{ browserOptions={ userVisibleOnly: true, applicationServerKey: convertedVapidKey }; } navigator.serviceWorker.ready.then(function (serviceWorkerRegistration) { serviceWorkerRegistration.pushManager. subscribe(browserOptions).then(function (subscription) { return sendSubscriptionToServer(subscription); }) .catch(function (e) { if (Notification.permission === 'denied') { console.warn('Permission for Notifications was denied'); } else { alert('Unable to subscribe to push.'+ e); } }); }); } </code> </pre>' ##And below is code for conversion from base64url to uint8Array ##' <pre><code> async function urlB64ToUint8Array(base64String){ const padding = '='.repeat((4 - base64String.length % 4) % 4); const base64 = (base64String + padding) .replace(/\-/g, '+') .replace(/_/g, '/'); const rawData = window.atob(base64); const outputArray = new Uint8Array(rawData.length); for (let i = 0; i < rawData.length; ++i) { outputArray[i] = rawData.charCodeAt(i); } console.log("output array is : "+outputArray); return outputArray; } </code> </pre> 作为您的映射器函数,而不是 RootFolder |-WebCOntent |-index.html |-main.js |-sw.js |-manifest.json

当前函数的问题在于它假设movie => movie.id总是碰巧是movie => Object.values(movie)[0]返回的Array中的第一个属性。对于当前编写的函数,这确实是正确的,但是我不确定在一般情况下您是否一定可以保证这一点。即使属性的顺序不同,直接引用id也可以。这也应该更快一些,因为您不必每次都将eaxh单个对象转换为Array。

答案 3 :(得分:0)

我认为这里的Object.values部分不需要使用map。没有它会是一样的:

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

答案 4 :(得分:0)

也许您可以使用更多核心版本。 在我的解决方案中,循环将仅运行一次。

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