在笛卡尔输出中重命名对象键

问题描述 投票:0回答:2

抱歉这个糟糕的问题标题,无法想象一个更好的问题。

我有这个选项数组:

const options = [
  {
    display_name: "Size",
    _id: "1",
    values: [
      {
        label: "Small",
        _id: "1"
      },
      {
        label: "Extra Large",
        _id: "2"
      }
    ]
  },
  {
    display_name: "Colors",
    _id: "2",
    values: [
      {
        label: "Red",
        value: "#ff0000",
        _id: "3"
      },
      {
        label: "Green",
        value: "#00ff21",
        _id: "4"
      },
    ]
  }
];

我对它运行此函数以获取Cartesian Product

const getCartesian = object => {
  return Object.entries(object).reduce(
    (r, [key, value]) => {
      let temp = [];
      r.forEach(s =>
        (Array.isArray(value) ? value : [value]).forEach(w =>
          (w && typeof w === "object" ? getCartesian(w) : [w]).forEach(x =>
            temp.push(Object.assign({}, s, { [key]: x }))
          )
        )
      );
      return temp;
    },
    [{}]
  );
};

这将导致以下格式的对象数组(console.log输出):


[{0: Object, 1: Object}, {0: Object, 1: Object}, ...]

所需的输出是:

[
            {
               "option":{
                  "id":1,
                  "display_name":"Size"
               },
               "value":{
                  "label":"Small",
                  "id": 1
               }
            },
            {
               "option":{
                  "id":2,
                  "display_name":"Color",
               },
               "value":{
                  "id":5,
                  "label":"Red"
               }
            }
...
]

这是游乐场和我到目前为止所尝试的:https://codesandbox.io/s/8nvwm76nnj

javascript cartesian-product
2个回答
1
投票

你需要在最后将map()转换为对象。

const options = [
  {
    display_name: "Size",
    _id: "1",
    values: [
      {
        label: "Small",
        _id: "1"
      },
      {
        label: "Extra Large",
        _id: "2"
      }
    ]
  },
  {
    display_name: "Colors",
    _id: "2",
    values: [
      {
        label: "Red",
        value: "#ff0000",
        _id: "3"
      },
      {
        label: "Green",
        value: "#00ff21",
        _id: "4"
      },
    ]
  }
];

const getCartesian = object => {
      let t = Object.entries(object).reduce(
        (r, [key, value]) => {
          let temp = [];
          r.forEach(s =>
            (Array.isArray(value) ? value : [value]).forEach(w =>
              (w && typeof w === "object" ? getCartesian(w) : [w]).forEach(x =>
                temp.push(Object.assign({}, s, { [key]: x }))
              )
            )
          );
          return temp;
        },
        [{}]
      );
      return t.map(({0:val1,1:val2}) => ({option:val1,arr:val2}))
    };
console.log(getCartesian(options));

1
投票

您可以使用属性option将数组包装在对象中。这将为您提供一个数组,其中包含option作为笛卡儿积的关键字的对象。

const getCartesian = object => {
    return Object.entries(object).reduce(
        (r, [key, value]) => {
            let temp = [];
            r.forEach(s =>
                (Array.isArray(value) ? value : [value]).forEach(w =>
                    (w && typeof w === "object" ? getCartesian(w) : [w]).forEach(x =>
                        temp.push(Object.assign({}, s, { [key]: x }))
                    )
                )
            );
            return temp;
        },
        [{}]
    );
};
const options = [{ display_name: "Size", _id: "1", values: [{ label: "Small", _id: "1" }, { label: "Extra Large", _id: "2" }] }, { display_name: "Colors", _id: "2", values: [{ label: "Red", value: "#ff0000", _id: "3" }, { label: "Green", value: "#00ff21", _id: "4" }] }];

console.log(getCartesian({ option: options }));
.as-console-wrapper { max-height: 100% !important; top: 0; }
© www.soinside.com 2019 - 2024. All rights reserved.