javascript中对象的笛卡尔积

编程入门 行业动态 更新时间:2024-10-10 05:20:27
本文介绍了javascript中对象的笛卡尔积的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要根据N个属性列表生成一组完整的变体,同时保持属性名称不变。

I need to generate a complete set of variants based on a list of N attributes, while keeping the attribute name intact.

var input = [ { 'colour' : ['red', 'green'] }, { 'material' : ['cotton', 'wool', 'silk'] }, { 'shape' : ['round', 'square', 'rectangle'] } ]; var expected = [ { 'colour': 'red', 'material': 'cotton', 'shape': 'round' }, { 'colour': 'red', 'material': 'cotton', 'shape': 'square' }, { 'colour': 'red', 'material': 'cotton', 'shape': 'rectangle' }, { 'colour': 'red', 'material': 'wool', 'shape': 'round' }, { 'colour': 'red', 'material': 'wool', 'shape': 'square' }, { 'colour': 'red', 'material': 'wool', 'shape': 'rectangle' }, { 'colour': 'red', 'material': 'silk', 'shape': 'round' }, { 'colour': 'red', 'material': 'silk', 'shape': 'square' }, { 'colour': 'red', 'material': 'silk', 'shape': 'rectangle' }, { 'colour': 'green', 'material': 'cotton', 'shape': 'round' }, { 'colour': 'green', 'material': 'cotton', 'shape': 'square' }, { 'colour': 'green', 'material': 'cotton', 'shape': 'rectangle' }, { 'colour': 'green', 'material': 'wool', 'shape': 'round' }, { 'colour': 'green', 'material': 'wool', 'shape': 'square' }, { 'colour': 'green', 'material': 'wool', 'shape': 'rectangle' }, { 'colour': 'green', 'material': 'silk', 'shape': 'round' }, { 'colour': 'green', 'material': 'silk', 'shape': 'square' }, { 'colour': 'green', 'material': 'silk', 'shape': 'rectangle' } ];

对于阵列的笛卡尔积,有很多算法,但我似乎找不到一个对于保留密钥的对象。

There are lots of algorithms around for cartesian products of arrays, but I can't seem to find one for objects that preserves the keys.

性能不是一个大问题,因为每个属性永远不会有超过十几个值。订单不必完全匹配预期。

Performance isn't a massive concern as there will never be more than a dozen or so values for each attribute. The order doesn't have to exactly match expected.

我已根据标准进行了初步尝试列表算法,但我很挣扎:

I've made an initial attempt based on the standard algorithms for lists, but I'm struggling:

function cartesianProduct(input, current) { if (!input || input.length < 1) { return []; } var head = input[0]; var tail = input.slice(1); var output = []; for (var key in head) { for (var i = 0; i < head[key].length; i++) { if (typeof current == 'undefined') { var current = {}; } current[key] = head[key][i]; var productOfTail = cartesianProduct(tail, current); output.push(current); console.log(current); } } return output; } console.log(cartesianProduct(input));

推荐答案

一旦你摆脱''我'是一个全局变量问题',您可以使用以下代码获得结果:

Once you get rid of the ' 'i' is a global var issue', you can get to the result with this code for instance :

var input = [ { 'colour' : ['red', 'green'] }, { 'material' : ['cotton', 'wool', 'silk'] }, { 'shape' : ['round', 'square', 'rectangle'] } ]; function cartesianProduct(input, current) { if (!input || !input.length) { return []; } var head = input[0]; var tail = input.slice(1); var output = []; for (var key in head) { for (var i = 0; i < head[key].length; i++) { var newCurrent = copy(current); newCurrent[key] = head[key][i]; if (tail.length) { var productOfTail = cartesianProduct(tail, newCurrent); output = output.concat(productOfTail); } else output.push(newCurrent); } } return output; } function copy(obj) { var res = {}; for (var p in obj) res[p] = obj[p]; return res; } console.log(cartesianProduct(input));

更多推荐

javascript中对象的笛卡尔积

本文发布于:2023-11-28 21:39:13,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644038.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:笛卡尔   对象   javascript

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!