0

How can I increment a variable suffix while using console.log?

For instance - printing delta0-2

var delta0 = "omega"
var delta1 = "orion"
var delta2 = "kiwi"

I was trying to use a for loop to add a number to the end of "delta" but that doesn't seem to work? For instance this would be for a long list of items. I just want to "print all variables".

5
  • Why don't you use an object with bracket notation? Commented Mar 8, 2017 at 8:28
  • Why don't you use Array for delta? Commented Mar 8, 2017 at 8:28
  • 1
    @GerardoFurtado: Maybe the Op inherited a legacy codebase and is trying to make some sense of the existing maze of variables. ;) Commented Mar 8, 2017 at 8:29
  • 1
    @Abhitalks You've got a point here, I've never thought about it before. Commented Mar 8, 2017 at 8:42
  • Thanks for the down vote whoever did that.. just a simple question. Tried to simplify it and not add in all of the complexities of the problem. Commented Mar 8, 2017 at 8:44

2 Answers 2

2

You can use eval

Like this

for (var i = 0; i < 3; i++)
  console.log(eval("delta" + i))

but eval is evil

But you can create object and access it like this

var obj = {
  delta0: "omega",
  delta1: "orion",
  delta2: "kiwi"
};
for (var i = 0; i < Object.keys(obj).length; i++)
  console.log(obj["delta" + i])
Sign up to request clarification or add additional context in comments.

6 Comments

Its better to use an object and then do obj["delta" + i]
@Rajesh Thanks for the suggestion.
It is all good, but it would be useful if you could explain why the Op is having a problem in the first place. Why just concatenating numbers is not working. Why do you suggest eval? What does eval do? Better alternatives can come next.
@Abhitalks Why don't you post answer instead of commenting? I think that would be better for everyone
It's ok @AnikIslamAbhi. I upvoted your answer because it was better than the other one, and just suggested an improvement. Its your choice to take it or leave it. I don't intend to add an answer just for the sake of it. Thanks.
|
2

You should use an array of string: var delta = ["omega", "orion", "kiwi"];

Then you can select each item like this:

var delta = ["omega", "orion", "kiwi"];

console.log("--select an element--");
console.log(delta[0]); // print omega
console.log(delta[1]); // print orion
console.log(delta[2]); // print kiwi

console.log("--Iterate all elements--");
delta.forEach(d => {
  console.log(d); // print omega, orion, and then kiwi
});

2 Comments

Please notice <> icon in editor. You can create a snippet using it
It is better to first solve the problem at hand and point out what the Op is doing wrong and why is he facing that problem. After having done that, add better alternatives to the answer. You have just pointed out an alternative, but made no attempt to actually answer the question Op asked.

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.