简体   繁体   English

将 javascript 中的字符串乘以 arrays,最后不加入它们

[英]Multiply arrays of strings in javascript, not joining them at the end

iam trying to create a list of products varinats from a list of selected options and options values i have data looks like this:我正在尝试从所选选项和选项值列表中创建产品变量列表,我拥有的数据如下所示:

[
  {
   option: "size",
   selected_values: [{id: 1, value: "XL"}, {id: 2, value: "M"}]
  },
  {
   option: "color",
   selected_values: [{id: 14, value: "Red"}, {id: 15, value: "Blue"}]
  }
]

and from this data i want t create somthing like this:从这些数据中我想创建这样的东西:

[
 {
 varint_name: "XL Red",
 selected_values: [1, 14]
 },
 {
 varint_name: "XL Blue",
 selected_values: [1, 15]
 },
 {
 varint_name: "M Red",
 selected_values: [2, 14]
 },
 {
 varint_name: "M Blue",
 selected_values: [3, 15]
 }
]

taking into considration that the list of selected options and options values (first list) can be more than two objects its binded to a from so can be more or less iam using vue btw and i know that the list i want should be a computed value but i want to know the logic to genrate the desired shape of list考虑到所选选项和选项值的列表(第一个列表)可以是两个以上的对象,它绑定到一个 from 所以可以或多或少使用 vue btw 的 iam,我知道我想要的列表应该是一个计算值但我想知道生成所需列表形状的逻辑

As suggested, you will need to generate the Cartesian product of all option values.如建议的那样,您将需要生成所有选项值的笛卡尔积。

I added a third option to showcase that this works for any given number of options.我添加了第三个选项来展示这适用于任何给定数量的选项。

 const options = [{ option: "size", selected_values: [ { id: 1, value: "XL" }, { id: 2, value: "M" } ] }, { option: "color", selected_values: [ { id: 14, value: "Red" }, { id: 15, value: "Blue" } ] }, { option: "length", selected_values: [ { id: 30, value: "Short" }, { id: 31, value: "Long" } ] }]; const main = () => { const product = cartesian(...options.map(o => o.selected_values)).map(values => ({ variant_name: values.map(({ value }) => value).join(' '), selected_values: values.map(({ id }) => id), })); console.log(product); }; const cartesian = (...args) => { const result = []; __cartesianHelper(result, [], 0, args); return result; } const __cartesianHelper = (result, curr, index, all) => { for (let j = 0, l = all[index].length; j < l; j++) { const copy = [...curr, all[index][j]]; if (index === all.length - 1) result.push(copy); else __cartesianHelper(result, copy, index + 1, all); } }; main();
 .as-console-wrapper { top: 0; max-height: 100%;important; }

Expected Output预计 Output

[{
  variant_name: "XL Red Short",
  selected_values: [1, 14, 30]
}, {
  variant_name: "XL Red Long",
  selected_values: [1, 14, 31]
}, {
  variant_name: "XL Blue Short",
  selected_values: [1, 15, 30]
}, {
  variant_name: "XL Blue Long",
  selected_values: [1, 15, 31]
}, {
  variant_name: "M Red Short",
  selected_values: [2, 14, 30]
}, {
  variant_name: "M Red Long",
  selected_values: [2, 14, 31]
}, {
  variant_name: "M Blue Short",
  selected_values: [3, 15, 30]
},  {
  variant_name: "M Blue Long",
  selected_values: [3, 15, 31]
}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM