gpt4 book ai didi

javascript - 拆分对象键名(用逗号分隔)生成新的对象数组

转载 作者:行者123 更新时间:2023-12-03 00:07:57 24 4
gpt4 key购买 nike

给定以下对象数组 - 从 D3 解析的传入 CSV 文件生成。例如,您会注意到 key[4] 由两个术语的字符串/名称组成,即基于社区(一般)和基于生态系统(一般)。我想重新创建这个对象数组,分割 key[4] (以及其他具有多个术语的对象)。由于 key[4] 由 Key[3] 和 Key[6] 组成,因此我需要将它们的 value.length 都增加 1。

我希望这是有道理的。

(14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}]
0: {key: "Not applicable", values: Array(122)}
1: {key: "Other ", values: Array(8)}
2: {key: "Green/blue infrastructure", values: Array(1)}
3: {key: "Community-based (general)", values: Array(3)}
4: {key: "Community-based (general), Ecosystem-based (general)", values: Array(1)}
5: {key: "Ecosystem-based adaptation", values: Array(9)}
6: {key: "Ecosystem-based (general)", values: Array(4)}
7: {key: "Community-based (general), Ecosystem-based adaptation", values: Array(1)}
8: {key: "Forest landscape restoration", values: Array(6)}
9: {key: "Community-based adaptation", values: Array(2)}
10: {key: "Community-based (general), Not applicable", values: Array(1)}
11: {key: "Nature-based (general)", values: Array(2)}
12: {key: "Integrated flood management", values: Array(1)}
13: {key: "Integrated coastal zone management", values: Array(2)}

我在上面的数组上尝试了以下循环:

dropDownValues.forEach(function(object, index){
var newObject = {}
var keyString = object.key
var value = object.values.length;

if (keyString.indexOf(',') != -1) {
value = object.values.length;
var segments = keyString.split(', ');
segments.forEach(function(segment){
newObject.key = segment;
newObject.values = value
})
}
if (keyString.indexOf(',') == -1) {
newObject.key = keyString;
newObject.values = value;
}
}

这会产生以下结果:

0: {key: "Not applicable", values: 122}
1: {key: "Other ", values: 8}
2: {key: "Green/blue infrastructure", values: 1}
3: {key: "Community-based (general)", values: 3}
4: {key: "Ecosystem-based (general)", values: 1}
5: {key: "Ecosystem-based adaptation", values: 9}
6: {key: "Ecosystem-based (general)", values: 4}
7: {key: "Ecosystem-based adaptation", values: 1}
8: {key: "Forest landscape restoration", values: 6}
9: {key: "Community-based adaptation", values: 2}
10: {key: "Not applicable", values: 1}
11: {key: "Nature-based (general)", values: 2}
12: {key: "Integrated flood management", values: 1}
13: {key: "Integrated coastal zone management", values: 2}

但是我需要增加值以合并拆分键名称。

最佳答案

  1. 将每个对象转换为对象数组。根据 key String.split() 执行此操作
  2. 使用 Array.flat() 展平您的数组
  3. 使用 Array.reduce() 将数组分组到一个对象下
  4. 使用 Object.entries() 将对象转换回您期望的数组和 Array.map()

这就是它的样子

const itemToArray = x => x.key.split(',').map(k => k.trim()).map(key => ({
key,
values: [...x.values]
}));

const toObj = arr => arr.map(itemToArray).flat().reduce((a, c) => {
if (!a[c.key]) { a[c.key] = []; }
a[c.key].push(...c.values);

return a;
}, {});

const groupArray = arr => Object.entries(toObj(arr)).map(([key, values]) => ({
key,
values
}));

const arr = [
{ key: "a", values: [1] },
{ key: "b", values: [2] },
{ key: "a, b", values: [3, 4] },
{ key: "c", values: [5] }
];

const grouped = groupArray(arr);

// Grouped array with lengths only
console.log(grouped.map(x => ({...x, values: x.values.length})));

// Grouped array with whole values
console.log(grouped);

关于javascript - 拆分对象键名(用逗号分隔)生成新的对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54875795/

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