gpt4 book ai didi

javascript - 遍历大对象也返回空值

转载 作者:行者123 更新时间:2023-11-30 06:13:01 24 4
gpt4 key购买 nike

我从互联网上复制了一个代码,它在 json 集合中搜索 - 找到相关的键并根据总数组或单个数组的操作返回它们的值。问题是它只搜索它找到的值并将它们相加。我想要一个附加功能来查找哪个键具有空值。如果有人可以调查一下,请。

function perform(keys, operation) {
function visit(object) {
Object.entries(object).forEach(([k, v]) => {
if (k in indices) return fn(result, indices[k], v);
if (v && typeof v === "object") visit(v);
});
}

var result = [],
indices = Object.assign({}, ...keys.map((k, i) => ({ [k]: i }))),
fn = {
notsum: function(target, key, value) {
if (target[key] === undefined) {
target[key] = value;
return;
}
if (!Array.isArray(target[key])) {
target[key] = [target[key]];
}
target[key].push(value);
},
sum: function(target, key, value) {
target[key] = (target[key] || 0) + value;
}
}[operation === "total" ? "sum" : operation];

visit(data);

//to add functionality here to get the keys with values, even if they are null

//end
return operation === "total" ? result.reduce((a, b) => a + b) : result;
}

工作原理

console.log(perform(["WA1", "WA3", "RAE1"], "notsum")); // [3, 2, 1]
console.log(perform(["WA1", "WA3", "RAE1"], "total")); // 6

JSON 对象可以简单也可以复杂

var data = {
version: "1.0",
submission: "editing",
WebData: {
WA1: 3,
WA3: 2,
WAX: "NEO",
WebGroup: [{ Web1: 3, Web2: 0 }, { Web1: 4, Web2: 1 }]
},
NonWebData: { NWA1: 3, NWA2: "INP", NWA3: 2 },
FormInputs: { FM11: 3, FM12: 1, FM13: 2 },
RawData: {
RawOverview: { RAE1: 1, RAE2: 1 },
RawGroups: [
{
name: "A1",
id: "1",
data: {
AD1: "period",
AD2: 2,
AD3: 2,
transfers: [
{ type: "in", TT1: 1, TT2: 2 },
{ type: "out", TT1: 1, TT2: 2 }
]
}
},
{
name: "A2",
id: "2",
data: {
AD1: "period",
AD2: 2,
AD3: 2,
transfers: [
{ type: "in", TT1: 1, TT2: 2 },
{ type: "out", TT1: 1, TT2: 2 }
]
}
}
]
},
Other: { O1: 1, O2: 2, O3: "hello" },
AddedBy: "name",
AddedDate: "11/02/2019"
};

该功能确实返回索引,其中我有所有键,但我想要键值对,所以我知道数组中的哪一个根本不存在。一个代码写在函数里给我

["WA1", "WA3", "RAE1"]
[2, null, null]

现在,它只查找值是否存在,然后将它们相加并给我 2 作为结果。

提前致谢

最佳答案

您可以编写一个函数来递归访问对象树中的所有节点并执行您想要的操作。下面是一个sum 和 'getBykey` 的示例函数。

function perform(obj, keys = [], operation = "getByKey") {
const results = [];
let sum = 0;

// Define a fucntion that traverses all nodes in your object tree
const visit = (obj, keys = [], operation = "getByKey") => {
if (obj && typeof obj === "object") {
Object.entries(obj).forEach(([key, value]) => {
if (keys.includes(key)) {
// You can add more if_block/switch_case for more operations
if (operation === "sum" && !isNaN(value)) {
sum += value;
} else {
results.push({ [key]: value });
}
}
visit(value, keys, operation);
});
}
}

// Traverse the object tree
visit(obj, keys, operation);

return operation === "sum" ? sum : results;
}

const getByKey = perform(data, ["WA1", "WA3", "RAE1", "AD1"]);
const sum = perform(data, ["WA1", "WA3", "RAE1"], 'sum');

console.log(sum);
console.log(getByKey);

关于javascript - 遍历大对象也返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57773937/

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