1

I have a question on what's the best way to achieve this, I have an array with 8 objects:

myArray = [ {id: 1, name, xxx, town: Town}, ...]

then I have a second array with another 8 objects of type Town:

townArray = [ {id: 1, name: RngTown}, ... ]

I subscribe to the first array, then I subscribe to the second, and what I want to achieve is a new array that fills the property Town of myArray, with the 8objects in townArray, order doesn't matter, it's just dummy data.

is map() the right way?

and in this example I have 8 objects in the first array, and 8 in the second, so each property Town will have a unique Town from the second array, but what would happen if I only had 6 town object? Can I map all of them to my 8objects, and when they finish after 6 towns, they start from the first town again until all objects in the first array have a town?

thank you

3
  • could you please eloborate what you want to achive. It's bit confusing at the moment. Do you want to update town in myArray with value from "townArray"? Commented Sep 20, 2018 at 8:44
  • exactly that, if myArray has 8 objects, and I have as well 8objects in the "townArray" of type Town, I want to assign to each town property, a town object from the second array, and would also like to know, if I didn't have 8 town objects in the second array, let's say I only had 6, how can I assign those 6 to the first 6 town properties, and the assign 2 more towns to the remaining 2 town properties? ofc at the end there will be town properties with the same town object as I would have only 6 different town objects, hope it s clear Commented Sep 20, 2018 at 8:49
  • let's say I only had 6, how can I assign those 6 to the first 6 town properties, and the assign 2 more towns to the remaining 2 town properties? meaning assigning town is random and there is no relationship between myArray and townArray, is that true? Commented Sep 20, 2018 at 8:54

1 Answer 1

1

I did an example here. I think this will solve your issue! What it does is assign the towns by the id, so the result 1 will have the town 1 and so on! stackblitz let me know if you really want to use map. Check in the console log the result!

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.