-6

I have two arrays object:

const arr1 = [{a: 'QQQ'}, {b: 'WWW'}]
const arr2 = [{a: 'EEE', b: 'RRR'}, {a: 'TTT', b: 'YYY'}]

Then I want the resulting array to be like this:

const results = [{'QQQ': 'EEE', 'WWW': 'RRR'}, {'QQQ': 'TTT', 'WWW': 'YYY'}]

Any way to do this? Thank you very much!

ABDULLOKH MUKHAMMADJONOV
  • 4,249
  • 3
  • 22
  • 40
  • 1
    Does this answer your question? [Mapping two arrays to get an array of object](https://stackoverflow.com/questions/67410731/mapping-two-arrays-to-get-an-array-of-object) – pilchard Mar 10 '22 at 10:28
  • 2
    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). Why is `arr1` an array of single-property objects? – Sebastian Simon Mar 10 '22 at 10:31
  • @pilchard No, my input is two arrays object – user11742091 Mar 10 '22 at 10:34
  • 1
    What have you tried so far and what are you having trouble with specifically? – Andy Mar 10 '22 at 10:34
  • 1
    @user11742091 it's a trivial difference in this case. Use the linked duplicate and figure out how to make it work with your inputs. – pilchard Mar 10 '22 at 10:36
  • @SebastianSimon element in arr1 have many properties, but I just need one property, then I just write single-property – user11742091 Mar 10 '22 at 10:37
  • @pilchard ok, let me try. Thanks – user11742091 Mar 10 '22 at 10:41
  • @user11742091 And what do the other properties do? What if the first object is `{ a: "QQQ", b: "WWW" }` instead of just `{ a: "QQQ" }`? Which of the two properties will be considered then? – Sebastian Simon Mar 10 '22 at 11:19
  • @SebastianSimon thanks. I combine two objects to only one, then I filter duplicate keys and get value. It's successful. – user11742091 Mar 10 '22 at 14:57

1 Answers1

0

Not sure if this is the most efficient way to do this, but it works for the example provided. This method relies on some relatively newer features of JavaScript, but ES6 is widely supported, so it hopefully won't be an issue in your case.

First, isolate the values in arr1 to be used as the object properties for the final result.

Then, re-map the objects of the second array by extracting the values from each object using Object.values() and reduce that to an object with the property names from the first array.

var arr1 = [{a: 'QQQ'}, {b: 'WWW'}];
var arr2 = [{a: 'EEE', b: 'RRR'}, {a: 'TTT', b: 'YYY'}];

var keys = arr1.reduce((valueArray,obj) => [...valueArray, ...Object.values(obj)],[]);

var results = arr2.map(o => Object.values(o).reduce((newObj,v,i) => ({...newObj,[keys[i]]:v}),{}),[])

console.log(results);
Jscrip
  • 309
  • 1
  • 7
  • 1
    var keys = Object.values(arr1).map(o => Object.values(o)) – Michel Lamsoul Mar 11 '22 at 00:59
  • That would work as well, and looks cleaner, but it made me realize I left out the spread operator on the Object.values() as the intention was to flatten the keys array. It's been a long couple days so I'm scratching my head over why keys works as a flattened array or 2d without changing anything in the next step. – Jscrip Mar 11 '22 at 01:23