简体   繁体   English

将一个数组中的值分配给嵌套数组的最有效方法

[英]Most performant way to assign values from one array into nested array

Given a collection ( users in the snippet below) with sub-collections, and another collection ( organizations ) that contains properties I need merged into the sub-collection of users , what is the most performant way to do so? 给定一个具有子集合的集合(下面的代码段中的users ),以及另一个包含我需要合并的属性的集合( organizations ),将其合并到users的子集合中,最有效的方法是什么?

The code below works, but nested iterations never feel that great to me. 下面的代码有效,但是嵌套迭代对我而言从来没有这么好。 Further, the production environment has hundreds / thousands of results, so performance is at least a consideration. 此外,生产环境具有数百/数千个结果,因此性能至少是一个考虑因素。

FYI, I'm leveraging the lodash library, and am aware some performance gain would be had from not using the library - however, I'm hoping someone can offer a more effective approach to the problem. 仅供参考,我正在利用lodash库,并且知道使用该库会带来一些性能提升-但是,我希望有人可以提供一种解决该问题的更有效方法。

The code below looks up the name of the organization and then adds that name to the users array for use in other parts of the app. 下面的代码查找组织的名称,然后将该名称添加到users数组以供应用程序的其他部分使用。

(The data structure is from an API, and cannot be changed.) (数据结构来自API,无法更改。)

 // organizations are actually fetched from an API via Observable. Can change at any time. const organizations = [{ organization_id: 5, organization_name: 'Sample 1' }, { organization_id: 6, organization_name: 'Sample 2' }, { organization_id: 9, organization_name: 'Sample 3' }, // etc... ]; // users are actually fetched from an API via Observable. Can change at any time. const users = [{ user_name: 'User 1', organizations: [{ organization_id: 9, organization_role: 'Limited' }, { organization_id: 6, organization_role: 'Admin' } ] }, { user_name: 'User 2', organizations: [{ organization_id: 5, organization_role: 'Limited' }, { organization_id: 6, organization_role: 'Limited' } ] }, // etc... ]; document.addEventListener('DOMContentLoaded', () => { _.forEach(users, (user) => { _.forEach(user.organizations, (organization) => { organization.organization_name = _.get(_.find(organizations, ['organization_id', organization.organization_id]), 'organization_name', '?'); }); }); console.log(users); }, false); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

Since _.find() is an O(n) operation (where n is the length of organizations const), and the forEach operations O(m * l) (m number of users, l average number of organisations for each user), the total operation is O(n * m * l). 由于_.find()是O(n)操作(其中n是组织const的长度),并且forEach操作O(m * l)(m个用户,l每个用户的平均组织数),总运算为O(n * m * l)。 You can lower it to O(n + m * l) by first mapping the organisations, which is an O(n) operation (using _.keyBy() for example). 您可以通过首先映射组织将其降低为O(n + m * l),这是一个O(n)操作(例如,使用_.keyBy() )。

 // organizations are actually fetched from an API via Observable. Can change at any time. const organizations = [{"organization_id":5,"organization_name":"Sample 1"},{"organization_id":6,"organization_name":"Sample 2"},{"organization_id":9,"organization_name":"Sample 3"}]; // users are actually fetched from an API via Observable. Can change at any time. const users = [{"user_name":"User 1","organizations":[{"organization_id":9,"organization_role":"Limited"},{"organization_id":6,"organization_role":"Admin"}]},{"user_name":"User 2","organizations":[{"organization_id":5,"organization_role":"Limited"},{"organization_id":6,"organization_role":"Limited"}]}]; document.addEventListener('DOMContentLoaded', () => { const orgDict = _.keyBy(organizations, 'organization_id'); _.forEach(users, (user) => { _.forEach(user.organizations, (organization) => { organization.organization_name = _.get(orgDict['organization_id'], 'organization_name', '?'); }); }); console.log(users); }, false); 
 <script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script> 

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

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