0

In my NodeJS app i get an JSON object within jade (which works fine). Here in i tried to collect(srcTweets) only the relevant data and return this in a new JSON object(stgTweets). Which i use for Dynamic Table use.

Here fore i use the following scrip:

    var srcTweets = [];
    var srcTweets = !{JSON.stringify(tweets)};
    var aantalSrcTweets = srcTweets.length;

    //Loop throug all tweets and collect all relevant elementen from srcTweet:
    var stgTweet = {}
    var stgTweets = [];
    console.info('----------START LOOP--------------------');
    for(var i = 0; i < aantalSrcTweets; i++){
        console.info(i);

        stgTweet.Id = srcTweets[i]._id;
        stgTweet.userId = srcTweets[i].user.id;
        stgTweet.userFollowerCount = srcTweets[i].user.followers_count;
        stgTweet.userFriendCount = srcTweets[i].user.friends_count;
        stgTweet.userFavouritesCount = srcTweets[i].user.favourites_count;
        stgTweet.text = srcTweets[i].text;
        stgTweet.coordinates = srcTweets[i].coordinates;
        stgTweet.userLocation = srcTweets[i].user.location;

        stgTweets[i] = stgTweet;

        console.info(stgTweets[i]);
        console.info(i);
    }
    console.info('----------END LOOP--------------------');

    //here i get the same items
    console.info(stgTweets[0]);
    console.info(stgTweets[1]);

When i print different index numbers of the array "stgTweet[0] and stgTweet[1]" the same data is showed. I tried to figure it out by logging the elements in the for loop , but that looks fine. And i realy don't know where to look futher.

How do i fill the array with different objects in stead of the same object, what happens in the script above.

Here is an example of the srcTweets:

[
   Object   {
      _id="56e19eb1ac5e621e0797c423",
      truncated=false,
      text="@Model_Symphony nu ja,
      s... prima Energiequelle...",
      more...
   },
   Object   {
      _id="56e1a73eac5e621e0797c424",
      truncated=false,
      text="Vandaag aangekondigd doo...",
      more...
   },
   Object   {
      _id="56e1a7b4ac5e621e0797c425",
      truncated=false,
      text="Mooi bedrijfsbezoek aan ...",
      more...
    }
]

1 Answer 1

1

The reason is because you're reusing the same object for every element in the array and objects are assigned by reference in Javascript, so stgTweets[0] === stgTweets[1].

What you could do instead is move your stgTweet declaration inside the loop (or just re-assign the value directly as shown below) so that a new object is created for each array element:

var stgTweets = new Array(aantalSrcTweets);
for (var i = 0; i < aantalSrcTweets; i++){
  stgTweets[i] = {
    Id: srcTweets[i]._id,
    userId: srcTweets[i].user.id,
    userFollowerCount: srcTweets[i].user.followers_count,
    userFriendCount: srcTweets[i].user.friends_count,
    userFavouritesCount: srcTweets[i].user.favourites_count,
    text: srcTweets[i].text,
    coordinates: srcTweets[i].coordinates,
    userLocation: srcTweets[i].user.location
  };
}
Sign up to request clarification or add additional context in comments.

1 Comment

Many thanks!! Your realy smart.. I can't accept the answer within 4 minutes. So i do that about 30 minutes if that is alread for you :-)

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.