0

I checked alot of topics around here considering the subject but the solutions do not seem to work for me. I guess I have some logic problem. My code is very simple. I have two arrays (The first contains three strings and the second contains three dates)

All I want to do is to store the first string in the first array with the first date in the second array in an object.

The problem with the code that the array I create in the function theMainFunc() is saving 3 objects with the last index of each array instead of matching them like I need.

Here is the code:

var dlcNameList = ['they shall not pass', 'in the name of the tsar', 'apocalypse'];
var dlcReleaseDate = [new Date(2017,2,28), new Date(2017,6,15), new Date(2017,11,25)];

function objectCreator (dlcObject,dlcName,dlcDate) {
		dlcObject.name = dlcName;
		dlcObject.date = dlcDate;
		return dlcObject;
}

function theMainFunc() {

	var dlcDetails = {};
	var i = 0;
	var storage = [];
	var x;

	for (i; i <= 2; i++){
	x = objectCreator(dlcDetails,dlcNameList[i],dlcReleaseDate[i]);
	storage[i] = x;
	}

	return storage; //This returns an array with three "Same" objects. Not what I want to really acheive
}

console.log(theMainFunc())

3 Answers 3

1

Here you go (:

Note: You don't have to create new object with every iteration, you can use Array#forEach and just push an object with already set properties and keys into the result array.

var dlcNameList = ['they shall not pass', 'in the name of the tsar', 'apocalypse'],
    dlcReleaseDate = [new Date(2017,2,28), new Date(2017,6,15), new Date(2017,11,25)],
    result = [];
    
    dlcNameList.forEach((v,i) => result.push({name : v, date : dlcReleaseDate[i]}));
    console.log(result);

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

2 Comments

Not sure what you mean by "You don't have to create new object with every iteration", as the literal creates a new object, it just does it inline.
@JustinBlank He was making a new object var dlcDetails = {} with every iteration and then assigning properties to it. This is basically not necessarily in this case.
1

Because dlcDetails is declared above the loop, you're accidentally reusing it and updating its values instead of pushing a new object to the array.

Note also that when you call a function with an object/array or set it as the value of another object's property, you do not pass a copy of that object, but instead sharing that object so that the caller sees modifications to it. You may see this referred to as "pass by reference" but that's a bit of a misnomer.

function theMainFunc() {
    var storage = [];

    for (var i = 0; i <= 2; i++){
        var dlcDetails = {};

        var x = objectCreator(dlcDetails,dlcNameList[i],dlcReleaseDate[i]);
        storage[i] = x;
     }

     return storage;
}

Comments

0

The problem with your code is that you pass to objectCreator() function the same object all 3 times (and you need to change i <= 2 to i < 2 as you start from 0) so your storage just has 3 same references to your object with properties that you override every time you call a function. Solution is to create new object inside objectCreator() and not pass it as a parameter.

2 Comments

you mean return an object directly without passing it as a parameter?
yes, creating new one inside the function not outside

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.