0

I am trying to get out of this, variables Car1 Car2, Car3, Car4 to be pushed to an array. But I can't seem to figure out how to increment a variable within a for loop. Any ideas?

function get_info() {
    n = 0;
    for (var option_post in postvalues) {
    n++;
        var pair = postvalues[option_post];
        var string_pair = pair.concat(": ")
        var new_postvalues = (string_pair.concat(document.getElementsByName(pair)[0].value));
        //Set up cars
        var make.concat(n) = document.getElementById("make").value;
        var year.concat(n) = document.getElementById("year").value;
        var model.concat(n) = document.getElementById("model").value;
        var car.concat(n) = "Make: ".concat(make.concat(n)).concat("-Model: ").concat(model.concat(n)).concat("-Year:").concat(year.concat(n)); // combine
    }
    cars.push(car1, car2, car3, car4);
}
Blake Connally
  • 689
  • 1
  • 4
  • 16
  • 1
    Might you be looking for the array index operator `[]`? E.g. `cars.push(car[0], car[1], car[2], car[3]);` – das-g Oct 28 '16 at 22:39
  • 1
    Could you explain what you hope to end up with? It looks like all of your calls to `document.getElementById` are going to come back the same for each iteration of the for-loop. Is that intentional? – LinuxDisciple Oct 28 '16 at 22:39
  • @LinuxDisciple Pretty straightforward if you read it, I need the Make, Year, Model for each car in an array. – Blake Connally Oct 28 '16 at 22:40
  • Where are your variables `Car1`, ... in that piece of code? That (broken) code seems unrelated to your question. Not clear, at least to me. – trincot Oct 28 '16 at 22:41
  • 3
    @trincot He's hoping that `var car.concat(n)` will declare a variable named `car1` when `n = 1`. – Barmar Oct 28 '16 at 22:41
  • Javascript doesn't have any way to do dynamic local variable names. You can do global variables with `window[name]`, but there's nothing similar for local variables. – Barmar Oct 28 '16 at 22:42
  • If you really, really, _really_ want dynamic variable names, see http://stackoverflow.com/questions/5117127/use-dynamic-variable-names-in-javascript . But I'm pretty sure your task (as far as I can guess from your snippet) doesn't actually need dynamic variable names. – das-g Oct 28 '16 at 22:43

2 Answers2

2

There is no reason to create a new identifier on each loop iteration, especially since you are looking to collect the values into an array.

function get_info() {
    var cars = [], car;
    for (var i = 0; i < postvalues.length; i++) {
        // ...

        // create a car
        car = "Make: " + document.getElementById('make' + i).value
            + "-Model: " + document.getElementById('model' + i).value
            + "-Year: " + document.getElementById('year' + i).value;

        // append it to the end of your array
        cars.push(car);
    }

    return cars;
}
Igor Raush
  • 15,080
  • 1
  • 34
  • 55
  • This would work but I need to change the ID of the div values. They also need to increment by one. make1, model1, year1, then make2, year2, model, 2 – Blake Connally Oct 28 '16 at 22:58
  • What is the type of `postvalues`? Is it an array? – Igor Raush Oct 28 '16 at 23:01
  • Please see my edit. To iterate over an array, you should be using a plain `for` loop rather than a `for-in` loop. This also allows you to use the loop index to generate the `
    ` IDs.
    – Igor Raush Oct 28 '16 at 23:03
  • This divs are still not increasing, showing an error each time saying it can't find the div. – Blake Connally Oct 28 '16 at 23:14
  • 1
    This means that your `
    ` IDs are not `make1`, `make2`, etc., they are something else. Can you post the HTML you are working with?
    – Igor Raush Oct 28 '16 at 23:26
  • Thank you for the help, I realized that since I was originally set to 0 in the for equation and I was naming my divs starting at 1 not 0. Simple mistake, thank you. Works perfect. – Blake Connally Oct 29 '16 at 20:17
0

I would create an object for each car, and then store them in another object keyed by the car name. Something along these lines:

var cars = {};

for(var i = 0; i < carData.length; i++) {
    var carRef = "car" + n;
    cars[carRef] = {};
    cars[carRef].make = carData[i].make;
    cars[carRef].year = carData[i].year;
    cars[carRef].model = carData[i].model;
}

It's not an exact match to your code, but hopefully you can get the idea from this example. With this, you would retrieve an individual car like this:

cars["car1"]

Or an individual property for a given car:

cars["car1"].make
Andrew D
  • 207
  • 1
  • 6
  • 1
    Instead of an object, use an array `cars = []`. – Barmar Oct 28 '16 at 22:43
  • He wants to use strings to reference cars though? – Andrew D Oct 28 '16 at 22:45
  • 1
    That's just internal to the function, he ends with `cars.push(car1, car2, ...)` – Barmar Oct 28 '16 at 22:45
  • Yeah I see that in his example, but I'm not sure if it being an array is necessary to his desired outcome. From the question title I got the impression he wanted to be able to reference "car1", "car2", "car3", etc. – Andrew D Oct 28 '16 at 22:48
  • The first sentence of the question say "variables car1, car2, ... to be pushed onto an array" – Barmar Oct 28 '16 at 22:52
  • Well I guess we are just interpreting things a little differently. You are looking more literally, and I'm making a judgement based on where I think the guy is trying to go. In any case, I think he's got plenty of ideas and suggestions to help him with now :) – Andrew D Oct 28 '16 at 22:55
  • Just notice that all the dynamically-named variables are local to the function. They'll be gone when the function returns, and all that's left is the array that the function is filling in. – Barmar Oct 28 '16 at 22:57
  • Yes, that's assuming that is what he is intending. It's hard to tell when someone is learning and figuring things out, and hasn't got their code working yet. It seems a bit strange to have the intent to dynamically name variables on each iteration of the loop, if you only need those variables temporarily to build a string and push it into an array. – Andrew D Oct 28 '16 at 23:03