Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
503 views
in Technique[技术] by (71.8m points)

javascript - 给定对象数组,计算定义了多少个(可能不同)属性(Given an array of objects, count how many (possibly different) properties are defined)

In a GeoJSON file, some properties are shared by all "features" (element) of the entire collection (array).(在GeoJSON文件中,某些属性由整个集合(数组)的所有“功能”(元素)共享。)

But some properties are defined only for a subset of the collection.(但是某些属性仅针对集合的子集定义。) I've found this question: [javascript] counting properties of the objects in an array of objects , but it doesn't answer my problem.(我发现了这个问题: [javascript]计算对象数组中对象的属性 ,但无法解决我的问题。)

Example:(例:)

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":[...]}}
];

expected result: a list a all existing properties and their cardinality, letting us know how (in)complete is the data-set.(预期结果:列出所有现有属性及其基数,让我们知道数据集的完成程度(不完整)。)

For instance:(例如:)
properties: 1000, properties.name: 1000, properties.zip: 890, properties.updated: 412,
geometry: 1000, geometry.type: 1000, geometry.coordinates: 1000

I have a (rather complicated) solution, but I do suspect that some people have already faced the same issue (seems a data science classic), with a better one (performance matters).(我有一个(相当复杂的)解决方案,但是我确实怀疑有些人已经遇到了同样的问题(似乎是数据科学的经典),而更好的问题(性能很重要)。)

Here is my clumsy solution:(这是我笨拙的解决方案:)

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

Therefore, there are three issues:(因此,存在三个问题:)

-step 1: this is much work (adding everything before removing most of it) for a rather simple operation.(-步骤1:对于一个相当简单的操作,这是很多工作(在删除大部分内容之前添加所有内容)。)

Moreover, this isn't recursive and second level is "manually forced".(而且,这不是递归的,第二级是“手动强制”的。)

-step 2, a recursive solution is probably a better one.(步骤2,递归解决方案可能是更好的解决方案。)

-May step 1 and 2 be performed in a single step (starting to count when a new property is added)?(-是否可以在单个步骤中执行步骤1和2(开始计算添加新属性的时间)?)

I would welcome any idea.(我欢迎任何想法。)

  ask by allez l'OM translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The JSON.parse reviver and JSON.stringify replacer can be used to check all key value pairs :(JSON.parse JSON.stringifyJSON.stringify器可用于检查所有键值对:)

 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 ) 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...