json 对象数组的笛卡尔积

发布于 2025-01-17 13:48:56 字数 852 浏览 2 评论 0原文

我如何在对象的数组中产生所有值的所有组合。

var input = [
    { "colour" : "red",
      "material" : "cotton" ,
       "shape" : "round" 
    },
  {   "colour" : "green",
      "material" : "wool" ,
       "shape" : "square" 
    }
];

预期的输出是所有可用选项的笛卡尔产品,创建了带有相同键的新数组。

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

How can I produce all of the combinations of the values in an array of the of Objects.

var input = [
    { "colour" : "red",
      "material" : "cotton" ,
       "shape" : "round" 
    },
  {   "colour" : "green",
      "material" : "wool" ,
       "shape" : "square" 
    }
];

The expected output is the Cartesian product of all the options available, creating a new array with the same keys.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱的故事 2025-01-24 13:48:56

我将分两个步骤:

步骤1:收集每个键的所有值,

var options = {};
input.forEach(function (item)
{
  for (var prop in item)
  {
    if (options[prop])
    {
      options[prop].push(item[prop]);
    } else
    {
      options[prop] = [item[prop]];
    }
  }
});
console.log(options);

这将给出

{ colour: [ 'red', 'green' ],
  material: [ 'cotton', 'wool' ],
  shape: [ 'round', 'square' ] }

步骤2:通过使用三重嵌套的迭代创建笛卡尔产品:

  let result = [{}];
  for (var prop in options)
  {
    // For each object in the existing list...
    result = result.map(function (object)    
    {
      // ... create copies of that object for each option for the current key
      return options[prop].map(function (option)
      {
        let newObject = Object.assign({}, object)
        newObject[prop] = option
        return newObject
      })

    })
      // The result is an array of arrays of objects, so flatten it
      .flat()
  }
  console.log(result)

它给出

[ { colour: 'red', material: 'cotton', shape: 'round' },
  { colour: 'red', material: 'cotton', shape: 'square' },
  { colour: 'red', material: 'wool', shape: 'round' },
  { colour: 'red', material: 'wool', shape: 'square' },
  { colour: 'green', material: 'cotton', shape: 'round' },
  { colour: 'green', material: 'cotton', shape: 'square' },
  { colour: 'green', material: 'wool', shape: 'round' },
  { colour: 'green', material: 'wool', shape: 'square' } ]

I would approach this in two steps:

Step 1: collect all values for each key

var options = {};
input.forEach(function (item)
{
  for (var prop in item)
  {
    if (options[prop])
    {
      options[prop].push(item[prop]);
    } else
    {
      options[prop] = [item[prop]];
    }
  }
});
console.log(options);

Which will give

{ colour: [ 'red', 'green' ],
  material: [ 'cotton', 'wool' ],
  shape: [ 'round', 'square' ] }

Step 2: Create the cartesian product by using a triple-nested iteration:

  let result = [{}];
  for (var prop in options)
  {
    // For each object in the existing list...
    result = result.map(function (object)    
    {
      // ... create copies of that object for each option for the current key
      return options[prop].map(function (option)
      {
        let newObject = Object.assign({}, object)
        newObject[prop] = option
        return newObject
      })

    })
      // The result is an array of arrays of objects, so flatten it
      .flat()
  }
  console.log(result)

Which gives

[ { colour: 'red', material: 'cotton', shape: 'round' },
  { colour: 'red', material: 'cotton', shape: 'square' },
  { colour: 'red', material: 'wool', shape: 'round' },
  { colour: 'red', material: 'wool', shape: 'square' },
  { colour: 'green', material: 'cotton', shape: 'round' },
  { colour: 'green', material: 'cotton', shape: 'square' },
  { colour: 'green', material: 'wool', shape: 'round' },
  { colour: 'green', material: 'wool', shape: 'square' } ]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文