0

Here's my input object —

{
    "email": [
      "abc"
    ],
    "name": [
      "def",
      "ghi"
    ],
    "number": [
      "123",
      "456"
    ]
}

Here's what I'm hoping to get as output —


[
  {
    "email":"abc",
    "name":"def",
    "number":"123"
  },
  {
    "email":"abc",
    "name":"ghi",
    "number":"123"
  },
  {
    "email":"abc",
    "name":"def",
    "number":"456"
  },
  {
    "email":"abc",
    "name":"ghi",
    "number":"456"
  }
]

And, here's my code —

const input = {
  "email": [
    "abc"
  ],
  "name": [
    "def",
    "ghi"
  ],
  "number": [
    "123",
    "456"
  ]
};

const keys = Object.keys(input);

const values = Object.values(input);
let depth = [];
let output = [];
values.forEach(value => depth.push(value.length));
depth = depth.reduce((a, b)=> a*b, 1);
let dict = {};
for (let i = 0; i < depth; i++) {
  for (let j = 0; j < keys.length; j++) {
    let key = keys[j];
    if (input[key][i] !== undefined) {
      dict[key] = input[key][i];
    }
  }
  console.log(dict);
  output.push(dict);
}
console.log(output);
trincot
  • 317,000
  • 35
  • 244
  • 286
Sourabh Choraria
  • 2,255
  • 25
  • 64

2 Answers2

2

Your approach by calculating the product of the lengths is certainly one that can work. But there are some problems with the implementation that make it fail:

  • With let dict = {}; you only create one object, and that object is being pushed to the result array repeatedly. Any mutation to that object will thus be seen in very entry of that result array. So you should at least create that dict object in every iteration of the outer loop

  • i will in many cases exceed the length of the input[key] array, so input[key][i] will be undefined. Yet you need to pick a value from that array. You should use modular logic to translate such i to a valid index, and then use the remainder of that i in the next iteration of the inner loop -- to pick a value from the next array.

Here is a slight adaptation of your code to tackle those issues. I also moved it into a function:

function cartesian(input) {
    let keys = Object.keys(input);
    let depth = Object.values(input).reduce((product, {length}) => product * length, 1);
    let result = [];
    for (let i = 0; i < depth; i++) {
        let j = i;
        let dict = {};
        for (let key of keys) {
            let size = input[key].length;
            dict[key] = input[key][j % size];
            j = Math.floor(j / size);
        }
        result.push(dict);
    }
    return result;
}

const input = {
  "email": [
    "abc"
  ],
  "name": [
    "def",
    "ghi"
  ],
  "number": [
    "123",
    "456"
  ]
};

let result = cartesian(input);
console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286
0

This answer is based on the answers provided on this question.

Step 1

Extract the three arrays:

const {email, name, number} = input;

Step 2

Perform the cartesian product:

const cartesian = (...a) => a.reduce((a, b) => a.flatMap(d => b.map(e => [d, e].flat())));
const product = cartesian(email, name, number);

Step 3

Recompose the output:

const output = product.map(triple => ({email: triple[0], name: triple[1], number: triple[2]}));

You can replace the cartesian function with other functions found in the related question, accordingly to the target version of ES you mean to support.

@pilchard proposes a more generic version, which doesn't need you to specify the properties, but just performs the cartesian product on all the ones available within an object, and it is:

const result = cartesian(...Object.entries(input).map(([k, vs]) => vs.map(v => [[k, v]]))).map(Object.fromEntries)
  • 1
    I didn't downvote but I guess the reason is obvious. Vote to close as a duplicate and don't copy and paste another answer. Multiple copies of the same answer aren't useful. – jabaa Apr 08 '22 at 10:50
  • It's an extension to the other answers. The other question is about cartesian product on arrays, here we talk about object properties. Chance are that if I just closed the question as a duplicate the OP was left confused and wouldn't know how to apply the information – Christian Vincenzo Traina Apr 08 '22 at 10:52
  • 1
    It also proposes a hardcoded solution based on preset properties rather than a more generic solution which would be simple enough. But mostly it's a duplicate, though I agree that implementation of the duplicate is confusing enough to warrant an answer here. – pilchard Apr 08 '22 at 10:52
  • Closing a question as a duplicate should happen when the questions are exactly the same, that means when the answers of a question can be transferred to the other question. This is not the case – Christian Vincenzo Traina Apr 08 '22 at 10:55
  • generic solution: `const result = cartesian(...Object.entries(input).map(([k, vs]) => vs.map(v => [[k, v]]))).map(Object.fromEntries);` [fiddle](https://jsfiddle.net/yga3ohsk/1/) – pilchard Apr 08 '22 at 10:57
  • @plichard you can add it as answer and I'd upvote it. My answer does what OP asked, a more generic version is appreciated for sure – Christian Vincenzo Traina Apr 08 '22 at 11:02
  • 1
    Feel free to add it to your answer, I think one reference to the cartesian product answer is enough. – pilchard Apr 08 '22 at 11:03