gpt4 book ai didi

javascript - 如何在lodash中从json数组中连接和查找?

转载 作者:行者123 更新时间:2023-12-01 00:43:51 25 4
gpt4 key购买 nike

我有一个要求,我需要将所有状态与每个区域连接起来,我在下面创建了一个示例:

但我不确定这是 lodash 中的最佳方式还是可以使用任何其他内置方法来更有效地创建。

var data = [ {
"region": "South",
"state": "Wyoming",
"sales": 1803
}, {
"region": "South",
"state": "Idaho",
"sales": 3380
}, {
"region": "South",
"state": "New Mexico",
"sales": 2779
}, {
"region": "South",
"state": "Montana",
"sales": 3589
}, {
"region": "South",
"state": "Utah",
"sales": 11223
}, {
"region": "South",
"state": "Oregon",
"sales": 17431
}, {
"region": "South",
"state": "Colorado",
"sales": 72110
}, {
"region": "South",
"state": "Arizona",
"sales": 45283
}, {
"region": "South",
"state": "Washington",
"sales": 88656
}, {
"region": "South",
"state": "California",
"sales": 55731
}, {
"region": "West",
"state": "Wyoming",
"sales": 1603
}, {
"region": "West",
"state": "New Mexico",
"sales": 4779
}, {
"region": "West",
"state": "Montana",
"sales": 5589
}, {
"region": "West",
"state": "Utah",
"sales": 11223
}, {
"region": "West",
"state": "Nevada",
"sales": 16729
}, {
"region": "West",
"state": "Oregon",
"sales": 17431
}, {
"region": "West",
"state": "Colorado",
"sales": 32110
}, {
"region": "West",
"state": "Arizona",
"sales": 35283
}, {
"region": "West",
"state": "Washington",
"sales": 138656
}, {
"region": "West",
"state": "California",
"sales": 457731
} ];
var cat = [];
_.forEach(_.uniq(_.map(data, 'region')), function(o){
_.forEach(_.uniq(_.map(data, 'state')), function(l) {
const fin = _.find(data, function(f) { return f.state == l && f.region == o; });

if (_.isUndefined(fin)) {
cat.push({'state': o + " - " + l, 'region': o, 'sales': 0});
} else {
cat.push({'state': o + " - " + l, 'region': o, 'sales': fin.sales});
}

});
});
console.log(cat);
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>

我使用了这个,因为上面的数据似乎南部地区缺少一些州,西部地区缺少一些州。但就我而言,我希望两个区域的状态数据相同,我使用了 lodash 函数,但我不确定这是否是最好的方法。

最佳答案

这是非常基本的 JavaScript,不需要 Lodash 或 jQuery。

您可以更新现有数组:

var data=[{"region":"South","state":"Wyoming","sales":1803},{"region":"South","state":"Idaho","sales":3380},{"region":"South","state":"New Mexico","sales":2779},{"region":"South","state":"Montana","sales":3589},{"region":"South","state":"Utah","sales":11223},{"region":"South","state":"Oregon","sales":17431},{"region":"South","state":"Colorado","sales":72110},{"region":"South","state":"Arizona","sales":45283},{"region":"South","state":"Washington","sales":88656},{"region":"South","state":"California","sales":55731},{"region":"West","state":"Wyoming","sales":1603},{"region":"West","state":"New Mexico","sales":4779},{"region":"West","state":"Montana","sales":5589},{"region":"West","state":"Utah","sales":11223},{"region":"West","state":"Nevada","sales":16729},{"region":"West","state":"Oregon","sales":17431},{"region":"West","state":"Colorado","sales":32110},{"region":"West","state":"Arizona","sales":35283},{"region":"West","state":"Washington","sales":138656},{"region":"West","state":"California","sales":457731}];

for (let d of data) {
d.state = `${d.region} - ${d.state}`;
}

console.log(data);

或者您可以创建一个新数组:

var data=[{"region":"South","state":"Wyoming","sales":1803},{"region":"South","state":"Idaho","sales":3380},{"region":"South","state":"New Mexico","sales":2779},{"region":"South","state":"Montana","sales":3589},{"region":"South","state":"Utah","sales":11223},{"region":"South","state":"Oregon","sales":17431},{"region":"South","state":"Colorado","sales":72110},{"region":"South","state":"Arizona","sales":45283},{"region":"South","state":"Washington","sales":88656},{"region":"South","state":"California","sales":55731},{"region":"West","state":"Wyoming","sales":1603},{"region":"West","state":"New Mexico","sales":4779},{"region":"West","state":"Montana","sales":5589},{"region":"West","state":"Utah","sales":11223},{"region":"West","state":"Nevada","sales":16729},{"region":"West","state":"Oregon","sales":17431},{"region":"West","state":"Colorado","sales":32110},{"region":"West","state":"Arizona","sales":35283},{"region":"West","state":"Washington","sales":138656},{"region":"West","state":"California","sales":457731}];

var newData = data.map(d => ({...d, state: `${d.region} - ${d.state}`}));

console.log(newData);

引用文献:

关于javascript - 如何在lodash中从json数组中连接和查找?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57536222/

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