简体   繁体   English

如何计算表中的对象数量

[英]How to count the number of objects in the table

How can I get count of objects in the table.如何获取表中对象的数量。 I want all objects looks like the same.我希望所有对象看起来都一样。 Eg in element "b" I don't have any objects, but in output I want to get all used objects with count 0.例如,在元素“b”中,我没有任何对象,但在 output 中,我想获取计数为 0 的所有已使用对象。

INPUT DATA
{
  a: {
    obj3: [{...}, {...}]
  },
  b: { },
  c: {
    obj1: [{...}, {...}, {...}]
    obj2: [{...}, {...}]
  }
}

OUTPUT DATA

{
  a: {
    obj1: 0,
    obj2: 0,
    obj3: 2
  },
  b: {
    obj1: 0,
    obj2: 0,
    obj3: 0
  },
  c: {
    obj1: 3
    obj2: 2,
    obj3: 0
  }
}

You can do something like this only if you want specific number of objX as output and the key name is in the same format.仅当您希望特定数量的objX为 output 并且密钥名称的格式相同时,您才能执行此类操作。

 let input = { a: { obj3: [{x:1}, {x:2}] }, b: { }, c: { obj1: [{x:1}, {x:2}, {x:3}], obj2: [{x:1}, {x:2}] } } let output = {}; for (const [key, value] of Object.entries(input)) { let result = {} for (let i = 1; i < 4; i++) { result['obj'+i] = value['obj'+i]? value['obj'+i].length: 0; } output[key] = result; } console.log(output);

To keep the code generic and a bit easier to manage and understand, I would loop over your data twice: first to discover all the unique keys you need to handle, and second to fill the structure with the right data.为了使代码保持通用并更易于管理和理解,我将循环遍历您的数据两次:首先发现您需要处理的所有唯一键,然后用正确的数据填充结构。

Getting the right keys获取正确的密钥

To get the right keys, I look at the values of the input object (we can ignore the a , b and c keys).为了获得正确的键,我查看了输入 object 的值(我们可以忽略abc键)。

We're interested in all keys of the objects in this layer.我们对这一层中对象的所有键感兴趣。 We can create a flat list of them using flatMap(Object.keys) .我们可以使用flatMap(Object.keys)创建它们的平面列表。

Because this list might contain duplicate keys, I store them in a Set .因为这个列表可能包含重复的键,所以我将它们存储在Set中。 This ensures all keys appear only once.这确保所有键只出现一次。 With the example data you provided, we now have a Set of "obj1", "obj2", "obj3" .使用您提供的示例数据,我们现在有一Set "obj1", "obj2", "obj3"

Transforming an object转换 object

We can now transform any object to an object that has all keys.我们现在可以将任何 object 转换为具有所有键的 object。 I capture this transformation in a Result constructor function.我在Result构造函数 function 中捕获了这种转换。

This function creates a list of all keys ( [...allKeys] ), map s over them, and checks our input object for the existence of the key (obj[k]? ).这个 function 创建了一个所有键的列表( [...allKeys] ), map s 在它们之上,并检查我们的输入 object 是否存在键(obj[k]? If the key exists, we use its length.如果密钥存在,我们使用它的长度。 If not, we default to 0 .如果没有,我们默认为0

Transforming the whole input转换整个输入

To transform your whole object, I defined a mapObj helper.为了转换你的整个 object,我定义了一个mapObj助手。 This takes an object, applies a function to each of its values, and returns those in a new object with the same keys.这需要一个 object,将 function 应用于其每个值,并使用相同的键返回新的 object 中的值。

 const input = { a: { obj3: [{}, {}] }, b: { }, c: { obj1: [{}, {}, {}], obj2: [{}, {}] } }; // Set(3) {"obj1", "obj2", "obj3"} const allKeys = new Set( Object.values(input).flatMap(Object.keys) ); const Result = obj => Object.fromEntries( [...allKeys].map( (k) => [ k, obj[k]?.length || 0 ] ) ); console.log( mapObj(Result, input) ) // Utils function mapObj(f, o) { return Object.fromEntries( Object.entries(o).map( ([k, v]) => [k, f(v)] ) ) }

This is my solution:这是我的解决方案:

 let input = { a: { obj3: [{x:1}, {x:2}] }, b: {}, c: { obj1: [{x:3}, {x:5}, {x:6}], obj2: [{x:7}, {x:8}] } } let output={}; Object.keys(input).forEach(key=>{ let item={obj1:0,obj2:0,obj3:0}; ["obj1","obj2","obj3"].forEach(itemType=>{ if (input[key][itemType]){ item[itemType]= input[key][itemType].length; } }) output[key]=item; }); console.log(output);

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

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