gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:46:58 25 4
gpt4 key购买 nike

在 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com