javascript - 给定一个对象数组,计算定义了多少(可能不同的)属性

标签 javascript arrays set counting

在 GeoJSON 文件中,某些属性由整个集合(数组)的所有“特征”(元素)共享。但是某些属性仅为集合的一个子集定义。 我发现了这个问题:[javascript] counting properties of the objects in an array of objects ,但它没有回答我的问题。

例子:

const features =
[ {"properties":{"name":"city1","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4] ...]}},
  {"properties":{"name":"city2","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4] ...]}},
  {"properties":{"name":"city3"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4] ...]]}},
// ... for instance 1000 different cities
  {"properties":{"name":"city1000","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[...]}}
];

预期结果:列出所有现有属性及其基数,让我们知道数据集的(不)完整程度。例如:

properties: 1000, properties.name: 1000, properties.zip: 890, properties.updated: 412,
geometry: 1000, geometry.type: 1000, geometry.coordinates: 1000

我有一个(相当复杂的)解决方案,但我确实怀疑有些人已经遇到过同样的问题(似乎是数据科学经典),并且有更好的解决方案(性能很重要)。

这是我笨拙的解决方案:

// 1: list all properties encountered in the features array, at least two levels deep
const countProps = af => af.reduce((pf,f) =>
                                            Array.from(new Set(pf.concat(Object.keys(f)))), []);
// adding all the properties of each individual feature, then removing duplicates using the array-set-array trick
const countProp2s = af => af.reduce((pf,f) =>
                                            Array.from(new Set(pf.concat(Object.keys(f.properties)))), []);
const countProp2g = af => af.reduce((pf,f) =>
                                            Array.from(new Set(pf.concat(Object.keys(f.geometry)))), []);

// 2: counting the number of defined occurrences of each property of the list 1
const countPerProp =  (ff) => pf => ` ${pf}:${ff.reduce((p,f)=> p+(!!f[pf]), 0)}`;
const countPerProp2s = (ff) => pf => ` ${pf}:${ff.reduce((p,f)=> p+(!!f.properties[pf]), 0)}`;
const countPerProp2g = (ff) => pf => ` ${pf}:${ff.reduce((p,f)=> p+(!!f.geometry[pf]), 0)}`;
const cardinalities = countProps(features).map((kk,i) => countPerProp(ff)(kk)) +
                      countProp2s(features).map(kk => countPerProp2s(ff)(kk)) +
                      countProp2g(features).map(kk => countPerProp2g(ff)(kk));

因此,存在三个问题:

-第 1 步:这是一项相当简单的操作(在删除大部分之前添加所有内容)。此外,这不是递归的,第二级是“手动强制”。

-第 2 步,递归解决方案可能是更好的解决方案。

-第1步和第2步是否可以一步完成(添加新属性时开始计算)?

我欢迎任何想法。

最佳答案

JSON.parse reviverJSON.stringify replacer可用于检查所有键值对:

var counts = {}, json = `[{"properties":{"name":"city1","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},{"properties":{"name":"city2","zip":1234}, "geometry":{"type":"polygon","coordinates":[[1,2],[3,4]]}},{"properties":{"name":"city3"},"geometry":{"type":"multiPolygon","coordinates":[[[1,2],[3,4]]]}},{"properties":{"name":"city1000","zip":1234,"updated":"May-2018"}, "geometry":{"type":"polygon","coordinates":[]}} ]`

var features = JSON.parse(json, (k, v) => (isNaN(k) && (counts[k] = counts[k] + 1 || 1), v))

console.log( counts, features )

关于javascript - 给定一个对象数组,计算定义了多少(可能不同的)属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59112093/

相关文章:

javascript - MediaElement.JS 和 Flash 的视频质量问题

javascript - p5.j​​s 中对象的 2D 网格

javascript - 如何使用一些运算符组合两个数组?

java - 读取并存储txt。数组列表中的文件内容

node.js - 如何使用 res.send() 返回 Set 或 Map Object

java - HashSet 没有序列化的 java spring roo

c++ - 剥离项目的最佳 C++ 容器?

c# - Controller 针对单个请求执行两次

javascript - 当 src 更改时, react img 不会立即更改。我怎样才能解决这个问题?

javascript - 使用纯 JavaScript 和 React.js