Combining 2 arrays

I wonder if someone could help? I have data returning from a twitter API call and have split it into 2 arrays.

twitterGetTweets(twIds)
                .then(response => {
                
                let tweets = response.data;
                let users = response.includes.users;

                tweets.map(item => {
                tweets.forEach(item => item._id = uuid.v4());
                })

                users.map(item => {
                users.forEach(item1 => item._id = uuid.v4());
                })
                            
                console.log(tweets);
                console.log(users); 

This comes from this original json return

“data”: [
{
“created_at”: “2022-07-20T15:03:58.000Z”,
“text”: “tweet 1 text”,
“id”: “1549772107999023104”,
“author_id”: “1127391888523300865”
},
{
“created_at”: “2022-07-20T19:04:53.000Z”,
“text”: “Tweet 2 text”,
“id”: “154983273…”,
“author_id”: “1127391888523300865”
},
{
“created_at”: “2022-07-20T21:30:15.000Z”,
“text”: “Tweet 3 text”,
“id”: “1549869318359449600”,
“author_id”: “1127391888523300865”
}
],
“includes”: {
“users”: [
{
“url”: “url details”,
“profile_image_url”: “https://pbs.tw…”,
“username”: “profile 1”,
“id”: “1127391888523300865”,
“name”: “username…”
}
]
}
}

In this example there is only one user returned, but this could be any number of users depending on the request sent to the API

What I need to do is combine the 2 arrays so that each of the tweet entries has the user data contained to produce

{
“created_at”: “2022-07-20T21:30:15.000Z”,
“text”: “Tweet 3 text”,
“id”: “1549869318359449600”,
“author_id”: “1127391888523300865”
“profile_image_url”: “https://pbs.tw…”,
“username”: “profile 1”,
“name”: “username…”
}

The filter point is the id field in the user and the author_id in the tweet. As the return of the user data is a variable depending on the request to the API. i.e. there could be 2 tweets from user 1 and 1 from user 2 etc. I am assuming there is some iteration on array to to push then into array 1, but sadly that’s beyond my current understanding on combining arrays.

Any help would be greatly appreciated.

Running 2 ARRAYS against eachother… something like…

let tweets = response.data;
let users = response.includes.users;

for (let a = 0; a < users.length;  a++) {
	let item1 = users[a];
	for (let b = 0; b < tweets.length;  b++) {
		let item2 = tweets[b];
		if(item1._id===item2._id) {console.log("MATCH!!!");}
	}
}

And additionaly you can create a new array → collecting all matching data…
let ArrayC = [];

ArrayC.push(MATCHED-DATA-HERE);

ArrayC-RESULT would be → [all, found, data, in, array, c]

Thanks so much for the prompt response.

I made a slight adjustment to the code as the link between the 2 arrays is between id in the user array and author_id in the tweet array, matches were found which is just the ticket.

Now what I’m aiming to do is create a third array that combines the data from the 2 arrays in the following manner:

{
“created_at” : “2022-07-20T21:30:15.000Z” , (tweet array)
“text” : “Tweet 3 text” , (tweet array)
“id” : “1549869318359449600” , (tweet array)
“author_id” : “1127391888523300865”, (tweet array)
“profile_image_url” : “https://pbs.tw…” , (user array)
“username” : “profile 1” , (user array)
" name" : “username…” (user array)
}

This is the bit I’m really struggling with as I’m new to array manipulation. Once I have this piece, I can really make some progress after almost 2 days of going around in circles.

Thanks so much for your help.

Your stamements are confusing!

I made a slight adjustment to the code as the link between the 2 arrays is between id in the user array and author_id in the tweet array , matches were found which is just the ticket.

id ----------------> userArray

author_id -----> tweetarray

This do not correspond to …
{
“created_at” : “2022-07-20T21:30:15.000Z” , (tweet array)
“text” : “Tweet 3 text” , (tweet array)
“id” : “1549869318359449600” , (tweet array)
“author_id” : “1127391888523300865”, (tweet array)
“profile_image_url” : “https://pbs.tw…” , (user array)
“username” : “profile 1” , (user array)
" name" : “username…” (user array)
}

Do everything step by step!

First step would be to find the right matches.
Define first which two values you are searching for (which ones can be used for comparison).

i gave you an example of 2-array running against each other.
Try this when, test it and understand it.

This will be your first step.

$w.onReady(() => {console.log("OnReady running...");
    let arrayA = [1,2,3,4,5,6,7,8,9]
    let arrayB = [3,6,9]

    for (let a = 0; a < arrayA.length;  a++) {
        let item1 = arrayA[a];
        for (let b = 0; b < arrayB.length;  b++) {
            let item2 = arrayB[b];
            if(item1 === item2) {console.log("MATCH!!!", arrayA[a]);}
        }
    }
});

Sorry if I confused, that wasn’t my intent and thank you for coming back.

If you look at the original json response, you will se that my 2 arrays were built on two specific elements. The data and the includes.users.

The object for me is to merge the 2 arrays in a way that passes the user data into the tweet data, so that this can then be passed to a repeater.

The join point in the arrays is the author_id on the tweets array and the id on the user array. I just noticed that they are shown as different in my suggested result.

Below is my objective for the single array.

{
“created_at” : “2022-07-20T21:30:15.000Z” , (tweet array)
“author_id” : “1127391888523300865”, (tweet array) (join)
“text” : “Tweet 3 text” , (tweet array)
“id” : “1549832737397391360” (tweet id) (tweet array)

“id” : “1127391888523300865” , (corrected) (user array) (join)
“username” : “profile 1” , (user array)
“profile_image_url” : “https://pbs.tw…” , (user array)
" name" : “username…” (user array)
}

The join items shown above are what I am hoping will provide a way to combine the 2 arrays into a single array containing the above construct , the above represents a single array from the 2 arrays.

Clearly the end result need not contain both the id and author_id field so just the id field would be fine, but that’s not essential as having both has no impact on my objective.

Your original code produces 3 matches exactly as expected which is fantastic. My end objective is to construct a single array with the above data then I can pass it to a repeater and that’s where I’m struggling.

tweets = response.data ;
users = response.includes.users ;

            **for**  ( let  a  =  0 ;  a  <  users.length ;   a ++) { 
            let  item1  =  users[a ]; 
            **for**  ( let  b  =  0 ;  b  <  tweets.length ;   b ++) { 
            let  item2  =  tweets[b ]; 
            if ( item1.id === item2.author_id ) { console . log ( "MATCH!!!" );} 

I can only apologise for the confusion, I know you must have better things to do than having to deal with a misleading response. I simply hadn’t noticed it.

Thanks again for sticking with it.

So now we are coming closer!

At first, now you have correct data…
{
“created_at” : “2022-07-20T21:30:15.000Z” , ( tweet array )
“author_id” : “1127391888523300865” , ( tweet array ) ( join )
" text" : “Tweet 3 text” , ( tweet array )
“id” : “1549832737397391360” ( tweet array )
//---------------------------------------------------------------------------------------------
“id” : “1127391888523300865” , ( user array ) ( join )
“username” : “profile 1” , ( user array )
“profile_image_url” : “https://pbs.tw…” , ( user array )
“name” : “username…” ( user array )
}

Second, you have seen how works my example.

So what is still missing to build your function ?

To understand your own code and what you are doing, you will need some BASIC-ELEMENTARY-KNOWLEDGE, so let me try to explain you some simple things.

  1. QUESTION: What is an ARRAY and how does it work ?

An array looks (is defined in JS like) …
let myArray = ;

You can fill your ARRAY with different type of VALUES, for example with…
a) STRINGS:


let myArray = ["STRING1", "string2", "String3", "StRiNg4"];

b) NUMBERS:

let myArray = [0, 1, 2, 3, 4, 5, 6, 7, ,8 ,9];

c) OBJECTS:

let myArray = [{OBJECT1}, {OBJECT2}, {OBJECT3}, {OBJECT4}];

How to get data out of an ARRAY ? ? ?

console.log(myArray[0]); --> RESULT --> {OBJECT1}
console.log(myArray[1]); --> RESULT --> {OBJECT2}
console.log(myArray[2]); --> RESULT --> {OBJECT3}
console.log(myArray[3]); --> RESULT --> {OBJECT4}

So now let us take a look onto your OBJECT:

{
        "created_at": "2022-07-20T21:30:15.000Z",       (tweet array)    
        "author_id": "1127391888523300865",             (tweet array) (join)
        " text": "Tweet 3 text",                        (tweet array)
        "id":   "1549832737397391360"                   (tweet array)
        "id": "1127391888523300865",                    (user array) (join)
        "username": "profile 1",                        (user array)
        "profile_image_url": "https://pbs.tw.......",   (user array)
        "name": "username..."                           (user array)
    }    

So which knowledge do you have righ now ?

  1. You know what an OBJECT is and how it is structured!
  2. You know what an ARRAY is and how it is working!
  3. You have the example with the 2-ARRAY-LOOPINGS where you let run 2 arrays against each other to get MATCHES!
  4. You also know now how to create a 3rd. ARRAY to get the wished data you need out of two ARRAYS.

What you want me to tell that you still don’t know how?
Yes you are right, i forgot to show you, so take a look here…

definition of 3rd. array:

let myThirdArray = [];
myThirdArray.push("STRING-X"); console.log(myThirdArray);
myThirdArray.push("STRING-Y"); console.log(myThirdArray);
myThirdArray.push("STRING-Z"); console.log(myThirdArray);
console.log(myThirdArray.length);

How to get a VALUE of an OBJECT inside an ARRAY ? ? ?
Oh oh oh, now it gets a little bit complicated!

let objectArray = [{"name": "Velo-Ninja", "profession": "PROGRAMMER"}]; 

console.log(objectArray[0]["name"]);
console.log(objectArray[0]["profession"]);

Do we now have all INGRIDIENTS to COOK our SOUP ???

NOPE !!!

You will still need the 2-LOOPS, already showed in my last examples!
And you will need an IF-ELSE-STATEMENT to filter the right MATCHES and FILL YOUR 3-rd ARRAY, exactly the way you want to.

So now, please first study all that, understand it and try to build your “Array-Double-Loop”

EXAMPLE:

let tweets = response.data;
let users = response.includes.users;

for (let a = 0; a < users.length;  a++) {
    let item1 = users[a];
    for (let b = 0; b < tweets.length;  b++) {
        let item2 = tweets[b];
        if(item1._id===item2._id) {console.log("MATCH!!!");}
    }
}
  1. Define your third ARRAY, which will catch all needed results (objects).

  2. Run your two ARRAYS (filled with OBJECTS) against each other in a double-loop! tweets/users.

  3. Create an IF-ELSE-STATEMENT, witch will recognize the MATCHES!

  4. If MATCH —> Push found VALUE/Object into your THIRD-ARRAY!
    The more MATCHES, THE MORE YOU WILL FILL YOUR THIRD ARRAY!

Try to put all information together and you will be able to create the function you want!

Thanks you for your extensive explanation. I will take some time out later today to work my way through it.