/    Sign up×
Community /Pin to ProfileBookmark

JavaScript recursive array merge like in PHP

Okay, I need a recursive merge function like the one in PHP

I have these arrays.

const myArray1 = [ [{ “hotel_id”: “84011815”, “city”: “Dubai” }, { “hotel_id”: “84011815”, “offer_id”: “43230076|1|2293” }, { “hotel_id”: “84011815”, “offer_id”: “16013804|4|1643” } ], [{ “hotel_id”: “84011815”, “city”: “Dubai” }, { “hotel_id”: “84011815”, “offer_id”: “28295947|1|1176” }, { “hotel_id”: “84011815”, “offer_id”: “4221802|3|2316” } ] ]

const myArray2 = [ [{ “hotel_id”: “84011815”, “city”: “Dubai” }, { “hotel_id”: “84011815”, “offer_id”: “43230076|1|2293” }, { “hotel_id”: “84011815”, “offer_id”: “16013804|4|1644” } ], [{ “hotel_id”: “84011815”, “city”: “Dubai” }, { “hotel_id”: “84011815”, “offer_id”: “28295947|1|1177” }, { “hotel_id”: “84011815”, “offer_id”: “4221802|3|2319” } ] ]

I want to merge them by hotel_id and make sure all offers are also merged into one resulting array… the result should be like this

[
[
{
“hotel_id”: “84011815”,
“city”: “Dubai”
},
{
“hotel_id”: “84011815”,
“offer_id”: “43230076|1|2293”
},
{
“hotel_id”: “84011815”,
“offer_id”: “16013804|4|1643”
},
{
“hotel_id”: “84011815”,
“offer_id”: “28295947|1|1176”
},
{
“hotel_id”: “84011815”,
“offer_id”: “4221802|3|2316”
}
]
]

any help?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rootFeb 10.2020 — Try looking at .filter, .map type functions that apply a rule or function to array contents, that way you can return a new array consisting of exactly the data you want to work on.
Copy linkTweet thisAlerts:
@kandreauthorFeb 10.2020 — @root#1614459 already tried those
Copy linkTweet thisAlerts:
@SempervivumFeb 10.2020 — Check if the function concat() does what you require.

Edit: Didn't look close enough, check this code:
const myArray1 = [[{ "hotel_id": "84011815", "city": "Dubai" }, { "hotel_id": "84011815", "offer_id": "43230076|1|2293" }, { "hotel_id": "84011815", "offer_id": "16013804|4|1643" }], [{ "hotel_id": "84011815", "city": "Dubai" }, { "hotel_id": "84011815", "offer_id": "28295947|1|1176" }, { "hotel_id": "84011815", "offer_id": "4221802|3|2316" }]]
const myArray2 = [[{ "hotel_id": "84011815", "city": "Dubai" }, { "hotel_id": "84011815", "offer_id": "43230076|1|2293" }, { "hotel_id": "84011815", "offer_id": "16013804|4|1644" }], [{ "hotel_id": "84011815", "city": "Dubai" }, { "hotel_id": "84011815", "offer_id": "28295947|1|1177" }, { "hotel_id": "84011815", "offer_id": "4221802|3|2319" }]]
const myArray3 = myArray1.concat(myArray2);
console.log(myArray3);
let result = [];
myArray3.forEach(function (item, idx) {
console.log(item)
result = result.concat(item);
});
console.log(result)
Copy linkTweet thisAlerts:
@rootFeb 12.2020 — @kandre#1614467 You have to remember that sorting objects is not the same as sorting an array.

Sorting an array of objects is simpler if you have an index property or a property that you could use as an index, like a date time stamp

So where .filter and .map type functions for arrays exist, then dealing with the elements of those arrays, to sort them, you have to look at the value of one of the properties in the object of that arrya element.

So...

you have
a = [{obj:4,value:7},{obj:14,value:1},
{obj:9,value:-4},{obj:3,value:7];

You would be looking at the array element a[n] which is an object, so the a[n].obj is what you sort on, not the a[n] as it won't result in anything.

Does that clarify my response further?
×

Success!

Help @kandre spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...