#Merge 2 arrays in a sorted array ?

54 messages · Page 1 of 1 (latest)

lone saddle
#

Hello guys, I'm trying to merge 2 arrays in one single sorted array, let me clarify it for you :


const PLAYERS = [
    { id: 1, name: 'Noob1', chatroom: [2, 3] },
    { id: 2, name: 'Noob2', chatroom: [4, 10] },
    { id: 3, name: 'Noob3', chatroom: [5] },
    { id: 4, name: 'Noob4', chatroom: [8, 6, 2, 7] },
    { id: 5, name: 'Noob5', chatroom: [9, 1, 4, 3, 11, 8, 6, 12] },
    { id: 6, name: 'Noob6', chatroom: [7, 10] },
    { id: 7, name: 'Noob7', chatroom: [8] },
    { id: 8, name: 'Noob8', chatroom: [3, 9] },
    { id: 9, name: 'Noob9', chatroom: [2, 6, 1, 7, 3, 4] },
    { id: 10, name: 'Noob10', chatroom: [5, 4] },
    { id: 11, name: 'Noob11', chatroom: [11] },
    { id: 12, name: 'Noob12', chatroom: [7, 4, 12] },


const ROOMS = [
    { id: 1, name: "ENG" },
    { id: 2, name: "DEU" },
    { id: 3, name: "ESP" },
    { id: 4, name: "CH" },
    { id: 5, name: "KR" },
    { id: 6, name: "JP" },

I'd like to have noob 1 in the ENG room, noob 2 in DEU etc etc. How would you do it ? Would you use spreead operator for that? Plus theres more indexes in my first array than my second one, so I assume I would use a filter method, right ?

grand tide
#

so you want ROOMS to override the name values from PLAYERS?

#

if so, you can do it easily with .map() and .find()

lone saddle
#

I don't know if override is the right terminology but some sort of merging ( sorry I'm not familiar with JS ).

At first I thought the concat method would work but I will try this way then

grand tide
#

if you can't get it, show what you tried and we can help

lone saddle
#

Ok, I was a bit hesitant because I was wondering if it was an object or an array and if the method related to what I want could work

lone saddle
grand tide
#

no

#

looking at the data again, are you sure you want "Noob1" to be in "ENG"? it says they're in "DEU" and "ESP".

lone saddle
#

Yes and so on, with Noob2 in DEU and Noob3 in ESP

grand tide
#

that's not what the data is indicating

#

you're matching user IDs to room IDs

#

the data indicates Noob1 is in 2 rooms, with the IDs 2 and 3

lone saddle
#

I mean yes but is there any way to match it with the ID ?

grand tide
#

you match the room IDs, not the user IDs

lone saddle
#

you're right, but I don't know how to do it

#

so I need to make a condition ?

#

no that would be too complicated and redondant

grand tide
#

you use .map() and .find()

#

rooms is a new property matching the IDs from chatroom to the names in ROOMS

lone saddle
#

so something like =

#

Sorry I'm trying to understand how to have this result with the map and find

grand tide
#

You map USERS. Inside of that, also map chatroom. Inside the inner map, match room ids (using find).

lone saddle
#

Ok I thin k I get it

#

To find the room id == > ```js

const RoomId = ( roomId ) => { const room = ROOMS.find(room => room.id === roomId );

return room ? room.name : " room not found ";

grand tide
#

You named your param room them used it as roomId. Otherwise yes

#

Your syntax is a bit wacky, but that’s the idea

lone saddle
#

To map the players =>

const PlayersWithRoom = PLAYERS.map(player => ({ ...player)};
grand tide
#

Show the full map code

#

No

lone saddle
#

God damn it, the syntax is damn wacky

grand tide
#

You don’t appear to be using map properly. Check the docs

lone saddle
#

const playersWithRoomNames = PLAYERS.map(player => ({
    ...player,
    chatroom: player.chatroom.map(getRoomNameById)
}));
#

wait is there an easier way to do it, the syntax is a bit complicated

grand tide
#

That looks better

lone saddle
#

Ngl for the second one I use chatgpt but I'm still bit confused

#

and then after that it since there's more indexes in one array than another I hvae to filter it

grand tide
#

You don’t need to filter anything

#

Stop thinking the two arrays match. They don’t.

#

User ids are not room ids

lone saddle
#

oooh right !!

#

the room id have already an array from index 1 to 12

#

by the way how you call this type of datastructure ? It's the first time I'm seeing that

#

This is an array that has an object but also has another array in a property

grand tide
#

USERS is an array of objects.
Those objects have a property that’s an array.

lone saddle
#

Ok this is a bit tricky I don't know if it's used that much and I've neber seen this type of datastructure

#

but wait a minute

#

this property ***id: 1 ***is seen as an array ?

grand tide
#

Arrays of objects are very common, yes.

#

No, id is not an array. chatroom is

lone saddle
#

ok ok so as long as there's only one property that contains an array it's called an array of objects

#

thank you so much for your clarification

grand tide
#

No. It’s an array of objects because it’s an array… of.. objects….

#

Those objects don’t have to have a property that’s an array…