gpt4 book ai didi

javascript - 如何暴力破解对象的值?

转载 作者:行者123 更新时间:2023-12-01 00:21:53 26 4
gpt4 key购买 nike

我有一组具有数字坐标 (x,y) 和名称的对象。

我想对某个范围内的每个 x 和 y 值进行暴力破解。 (说 [0,1,2,3,4,5])

var variables = {"aboutTitle_x": 0, "aboutTitle_y": 0}
let f = (a, b) => [].concat(...a.map(a => b.map(b => [].concat(a, b))));
let cartesian = (a, b, ...c) => b ? cartesian(f(a, b), ...c) : a;


var candidates = cartesian(Object.keys(variables), [0, 1,2,3,4,5]);
console.log(candidates);

我正在使用笛卡尔积,这给了我每项任务,如下所示:

["aboutTitle_x", 0]
["aboutTitle_x", 1]
["aboutTitle_x", 2]
["aboutTitle_x", 3]
["aboutTitle_x", 4]
["aboutTitle_x", 5]
["aboutTitle_y", 0]
["aboutTitle_y", 1]
["aboutTitle_y", 2]
["aboutTitle_y", 3]

如何迭代 (aboutTitle_x, aboutTitle_y) 的潜在组合。我想要的是这样的东西:

{aboutTitle_x: 0, aboutTitle_y: 0}
{aboutTitle_x: 0, aboutTitle_y: 1}
{aboutTitle_x: 0, aboutTitle_y: 2}
{aboutTitle_x: 0, aboutTitle_y: 3}
{aboutTitle_x: 0, aboutTitle_y: 4}

最佳答案

您可以使用递归方法,在值上使用 for 循环,但通过递归增加键,并且还将对象引用作为当前值传递。

var variables = {
"aboutTitle_x": 0,
"aboutTitle_y": 0
}

function cartesian(keys, values, k = 0, tmp = {}) {
const r = [];

if (k >= keys.length) {
r.push({ ...tmp });
return r;
}

for (let i = 0; i < values.length; i++) {
Object.assign(tmp, {[keys[k]]: values[i]})
r.push(...cartesian(keys, values, k + 1, tmp))
}

return r;
}


var candidates = cartesian(Object.keys(variables), [0, 1, 2, 3, 4, 5]);
console.log(candidates);

您还可以使用 reduce 方法而不是 for 循环来执行此操作。

var variables = {
"aboutTitle_x": 0,
"aboutTitle_y": 0
}

function cartesian(keys, values, k = 0, tmp = {}) {
return values.reduce((r, e) => {
tmp[keys[k]] = e;

if (k >= keys.length - 1) {
r.push({ ...tmp});
return r;
}

r.push(...cartesian(keys, values, k + 1, tmp))
return r;
}, [])
}


var candidates = cartesian(Object.keys(variables), [0, 1, 2, 3, 4, 5]);
console.log(candidates);

您还可以将递归与生成器结合使用,这样您就可以控制每次迭代。

var variables = {
"aboutTitle_x": 0,
"aboutTitle_y": 0
}

function* cartesian(keys, values, k = 0, tmp = {}) {
if (k >= keys.length) {
yield { ...tmp }
} else {
for (let i = 0; i < values.length; i++) {
Object.assign(tmp, { [keys[k]]: values[i] })
yield* cartesian(keys, values, k + 1, tmp)
}
}
}

var it = cartesian(Object.keys(variables), [0, 1, 2, 3, 4, 5]);

for (let i of it) {
console.log(i)
}

关于javascript - 如何暴力破解对象的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59342537/

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