-2

I need to get these two combinations from these two arrays like this.

const array1 = [ '0', '4', '8', '16', '24', '32', '40', '48', '56', '64' ]
const array2 = [ 'p', 'px', 'py', 'pt', 'pe', 'pb', 'ps', 'm', 'mx', 'my', 'mt', 'me', 'mb', 'ms']

// output 1
// const combine = {p: [p-0, p-4, p-8,...], px: [px-0, px-4, px-8,...].....}

// output 2
// const combineUpdate = {p: ["", p-0, p-4, p-8,...], px: ["", px-0, px-4, px-8,...].....} 
// add an empty string in each  array to get certain use case

// we can access values like this
// combine.p
// combine.px
// combineUpdate.ms
kumar004
  • 197
  • 1
  • 1
  • 6
  • 1
    and what have you tried so far? – Bravo Mar 09 '22 at 10:21
  • What is the question? Get familiar with [how to access and process objects, arrays, or JSON](https://stackoverflow.com/q/11922383/4642212) and how to [create objects](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Object_initializer) and use the static and instance methods of [`Object`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object#Static_methods) and [`Array`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array#Static_methods). – Sebastian Simon Mar 09 '22 at 10:21
  • + What are you **exactly** trying to do, besides vague code comments – codingmaster398 Mar 09 '22 at 10:22
  • 1
    Use two nested loops. The outer loop iterates over `array2` and the inner loop over `array1`. You can use `reduce` and `map` instead. – jabaa Mar 09 '22 at 10:24
  • `array2.reduce(($,_)=>({...$,[_]:array1.map($=>\`${_}-${$}\`)}),{})` – Bravo Mar 09 '22 at 10:31

1 Answers1

2

You could map the first array as key ad prewfix and get the values of the other array as postfix.

const
    array1 = [ '0', '4', '8', '16', '24', '32', '40', '48', '56', '64' ],
    array2 = [ 'p', 'px', 'py', 'pt', 'pe', 'pb', 'ps', 'm', 'mx', 'my', 'mt', 'me', 'mb', 'ms'],
    result = Object.fromEntries(array2.map(k => [k, array1.map(v => [k, v].join('-'))]));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392