1

I'm trying to fill an JS array using a loop but is not working This is my code:

words = ["one", "two", "three", "four"];
words_formated = [];

for( var i=0 ; i<words.length ; i++ ){
    words_formated.push({words[i]: "<i>"+ words[i] +"</i>"});
}
2
  • 4
    The syntax for arrays is [...] Commented Feb 7, 2014 at 9:59
  • 1
    try with words = ["one", "two", "three", "four"]; Commented Feb 7, 2014 at 9:59

5 Answers 5

1

I also got an error with trying to use the array key to directly assign the object property name, this worked:

words = ["one", "two", "three", "four"];
words_formated = [];

for( var i=0; i<words.length; i++ ){
    var item = words[i]
    words_formated.push({item: "<i>"+ words[i] +"</i>"});
}

jsfiddle here

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

Comments

1

As your are pushing an object into array words_formated.push({words[i]: "<i>"+ words[i] +"</i>"}); , the key name words[i] is not correct

The problem will get solved if you write

words_formated.push({words: "<i>"+ words[i] +"</i>"});

and its best practice if you define word array correctly as words = ["one", "two", "three", "four"];

Comments

1

Try this :

var words = ['one', 'two', 'three', 'four'],
    words_formated = [],
    item;
for (var i = 0; i < words.length; i++) {
    item = {};
    item[words[i]] = '<i>' + words[i] + '</i>';
    words_formated.push(item);
}

According to your own code, the result will be :

[
    { one   : "<i>one</i>"   }, 
    { two   : "<i>two</i>"   }, 
    { three : "<i>three</i>" }, 
    { four  : "<i>four</i>"  }
]

Array :

var a = (1, 2, 3); // a sequence of numbers (returns the last one)
a // 3
var a = [1, 2, 3]; // an array
a // [1, 2, 3]

Fails :

var k = ['name'];
var o = { k[0]: 'value' }; // SyntaxError: Unexpected token [

Passes :

var k = ['name'];
var o = {};
o[k[0]] = 'value';
o // Object {name: "value"}

8 Comments

Your solution is working as well. Very useful your explanation to use array / object . Thank you
sorry, I was not paying attention. I'm using: codevar item = words[i] words_formated.push({item: "<i>"+ words[i] +"</i>"})code
I think your solution is putting js objects in the array, isn't it?
@bito_ This is my last try. Which result do you want (choose a number) : #1 [{item:'<i>one</i>'}], #2 [{one:'<i>one</i>'}] or #3 [{words:'<i>one</i>'}]?
according to your choices I choose [{one:'<i>one</i>'}]
|
0
words = ("one", "two", "three", "four"); //wrong

Instead use within array block

words = ["one", "two", "three", "four"];

Comments

0

Use like an array:

words = ["one", "two", "three", "four"];
var words_formated=[];

for( var i=0 ; i<words.length ; i++ ){

    words_formated[words[i]]= "<i>"+ words[i] +"</i>";
}

console.log(words_formated);

2 Comments

Look at this jsFiddle: jsfiddle.net/a3mCu Press run for test it. It works...
If I try to 'stringify' your array I'll receive an empty array: alert(JSON.stringify(words_formated)); unless we declare words_formated as an object - var words_formated={}; Thank 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.