简体   繁体   English

比较两个对象并在javascript中提取具有相同键的对象

[英]Compare two objects and extract the ones that have same Key in javascript

I have the following two objects 我有以下两个对象

var productionTime= [
    {Rob3: 20},
    {Rob8: 100},
    {Rob4: 500},
    {Rob1: 100},
    {Rob5: 500}
];
var Busytime= [
    {Rob4: 10},
    {Rob3: 200},
    {Rob8: 100},
    {Rob5: 200},
    {Rob1: 100}
];

Now I want to divide each item in 'productionTime' by its respective 'BusyTime' which have the same key. 现在,我想将“ productionTime”中的每个项目除以各自具有相同键的“ BusyTime”。 For example productionTime.Rob3 should be divided by BusyTime.Rob3 and productionTime.Rob8 should be divided by BusyTime.Rob8 and so on. 例如,productionTime.Rob3应该除以BusyTime.Rob3,productionTime.Rob8应该除以BusyTime.Rob8,依此类推。

How can I do this with array.find() or array.filter() in javascript/nodejs? 如何使用javascript / nodejs中的array.find()或array.filter()做到这一点?

PS: I know i can do it by using two nested forEach loops but that is I guess very slow PS:我知道我可以通过使用两个嵌套的forEach循环来做到这一点,但是我猜这很慢

You could use a hash table and a single loop for every array. 您可以为每个数组使用一个哈希表和一个循环。

 var productionTime = [{ Rob3: 20 }, { Rob8: 100 }, { Rob4: 500 }, { Rob1: 100 }, { Rob5: 500 }]; busytime = [{ Rob4: 10 }, { Rob3: 200 }, { Rob8: 100 }, { Rob5: 200 }, { Rob1: 100 }], hash = Object.create(null); busytime.forEach(function (o) { var key = Object.keys(o)[0]; hash[key] = o[key]; }); productionTime.forEach(function (o) { var key = Object.keys(o)[0]; o[key] /= hash[key]; }); console.log(productionTime); 
 .as-console-wrapper { max-height: 100% !important; top: 0; } 

Convert both arrays to object using Object#assign and the spread syntax . 使用Object#assignspread语法将两个数组都转换为对象。 Get the keys from one of them using Object#keys , and iterate the keys using Array#map . 使用Object# keys从其中之一获取密钥 ,并使用Array#map迭代密钥。 Create a new object for each key using shorthand property names : 使用速记属性名称为每个键创建一个新对象:

 const productionTime = [{"Rob3":20},{"Rob8":100},{"Rob4":500},{"Rob1":100},{"Rob5":500}]; const Busytime= [{"Rob4":10},{"Rob3":200},{"Rob8":100},{"Rob5":200},{"Rob1":100}]; // create objects from both arrays const productionTimeObj = Object.assign({}, ...productionTime); const busytimeObj = Object.assign({}, ...Busytime); // get the keys from one of the objects, and iterate with map const result = Object.keys(productionTimeObj).map((key) => ({ // create a new object with the key, and the result of the division [key]: productionTimeObj[key] / busytimeObj[key] })); console.log(result); 

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

相关问题 合并两个多维对象,如果它们在javascript中具有相同的键值 - Merging two multidimensional objects if they have the same key value in javascript 比较JavaScript中的两个对象 - Compare two objects in JavaScript 比较两个对象并用相同的键替换值 - 保留/忽略 rest - Compare two objects and replace values with same key - keep/ignore the rest 如何比较数组中的每个元素并将具有相同数据的元素分组(在我的情况下为日期)? JavaScript - How to compare each elements in an Array and group the ones that have same data (in my case date) ? JavaScript 比较两个 arrays 并检查是否有相同的密钥 - Compare two arrays and check if have a same key's 如何使用 object 中的键比较 JavaScript 中对象的两个 arrays - How to compare two arrays of objects in JavaScript using a key in object 比较具有 boolean、字符串和 object 作为 javascript 值的两个对象? - Compare two objects that have boolean, string and object as value in javascript? 将具有相同键的多个对象的内容与该键作为 JavaScript 中的属性组合 - Combining the content of multiple objects that have the same key with that key as the property in JavaScript 我有两个定时器在 javascript 中不工作 - I have two timers ones not working in javascript 如果两个不同的对象具有相同的键名,则合并它们的值 - If two different objects have same key name, merge their value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM