0
const array1 = ['US', 'AG','In'] // (country code array)
const array2 = ['flagIconUS','flagIconAG','flagIconIN']

Result :

const array3 = [
  { code: "US", icon: "flagIconUS" },
  { code: "AG", icon: "flagIconAG" },
  { code: "IN", icon: "flagIconIN" },
];
5
  • 1
    Are the elements you wanna match have the same index in the two arrays? I mean in the same order? Commented Aug 17, 2021 at 11:41
  • 1
    Why use an Array when you can already make use of the ID-alike shortcode. An object seems way easier to retrieve stuff from and also easy to iterate over when needed using Object.entries etc. Commented Aug 17, 2021 at 11:42
  • 1
    Map 2 array into 1 array object Commented Aug 17, 2021 at 11:46
  • 1
    What have you tried so far to solve this on your own? A simple for loop would be enough to get the expected output... Commented Aug 17, 2021 at 11:50
  • 4
    Does this answer your question? Map 2 array into 1 array object Commented Aug 17, 2021 at 11:51

4 Answers 4

3

This is simple one-liner with map

const array1 = ["US", "AG", "In"];
const array2 = ["flagIconUS", "flagIconAG", "flagIconIN"];

const result = array1.map((code, i) => ({ code, icon: array2[i] }));
console.log(result);

Sign up to request clarification or add additional context in comments.

8 Comments

What happens when they have mismatching sizes?
According to OP's question and expected result. If there is a mismatch then OP would have specifically mentioned it in the question itself.
Yes, you fully answered the question. But I was wondering that it wouldn't hurt for people searching for this to elaborate on the edge cases as well.
@AntonKrug I really appreciate your concern but see we have a situation where my answer will cover it automatically. Let's say array1 length is 2 and array2 length is 3 then the result will be an array with 2 objects because it will iterate up to 2 and not includes the third element. Now second case is where there is array1 length is 3 and array2 is 2, In this case the result will show up with 3 elements and icon will be undefined, which I think is expected. So I think this is win-win situation... If I am missing something then please add up here...
@AntonKrug Please check my solution stackoverflow.com/questions/68816660/…
|
1

Using Array.prototype.reduce()

const array1 = ["US", "AG", "In"];
const array2 = ["flagIconUS", "flagIconAG", "flagIconIN"];

const result = array1.reduce((acc,item,index)=>{
   return acc.concat({code:item,icon:array2[index]})
},[]);
console.log(result);

Comments

1

You can use Array.from like this:

const array1 = ["US", "AG", "In"]; // (country code array)
const array2 = ["flagIconUS", "flagIconAG", "flagIconIN"];

const array3 = Array.from(
  { length: 3 },
  (_, i) => ({ code: array1[i], icon: array2[i] })
);

console.log(array3);

One advantage of this solution: it doesn't use one particular array as the starting point, and allows more options for cases when array lengths mismatch because you can set the length of the resulting array yourself (instead of a hard-coded 3 like I did).

Example with one superfluous item in array1:

const array1 = ["US", "AG", "In", "superfluous"]; // (country code array)
const array2 = ["flagIconUS", "flagIconAG", "flagIconIN"];

const array3 = Array.from(
  { length: Math.min(array1.length, array2.length) },
  (_, i) => ({ code: array1[i], icon: array2[i] })
);

console.log(array3);

The same code works when there is one superfluous item in array2:

const array1 = ["US", "AG", "In"]; // (country code array)
const array2 = ["flagIconUS", "flagIconAG", "flagIconIN", "superfluous"];

const array3 = Array.from(
  { length: Math.min(array1.length, array2.length) },
  (_, i) => ({ code: array1[i], icon: array2[i] })
);

console.log(array3);

1 Comment

Isn't that dynamic if you use { length: array1.length }
1

Array.reduce implementation

Logic

  • Create an array from 0 to 3 using Array.from(Array(Math.max(array1.length, array2.length)). This will create an array starting from 0 to maximum of first and second array. In our case its 3.
  • Reduce this array, current value inside the reducer function will be 0, 1 and 2. These are our required indices.
  • Push to accumulator array with key from first array and icon from second array.
    const array1 = ['US', 'AG', 'In', 'UK']; // (country code array)
    const array2 = ['flagIconUS', 'flagIconAG', 'flagIconIN'];
    const array3 = Array.from(Array(Math.max(array1.length, array2.length))
                    .keys()).reduce((acc, curr) => {
                      acc.push({
                        code: array1[curr] || '',
                        icon: array2[curr] || '',
                      })
                      return acc;
                    }, []);
    console.log(array3);

Array.map implementation.

Logic

  • Generate an array with the same logic as mentioned earlier.
  • From this generated array run Array.map function.
  • Return the code from array1 and icon from array2 with index from above array.

const array1 = ['US', 'AG', 'In', 'UK'];
const array2 = ['flagIconUS', 'flagIconAG', 'flagIconIN'];
const array3 = Array.from(Array(Math.max(array1.length, array2.length)).keys()).map((index) => ({
                  code: array1[index] || '',
                  icon: array2[index] || '',
                }));
console.log(array3);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.