1

I have an object named myInfo

var myInfo = {
            skills: ['PHP', 'Java', 'JavaScript', 'HTML', 'CSS', 'jQuery'],
            languages: ['English', 'German']
            }

And I want to loop over the strings inside the skills array which is inside the myInfo object. I've tried this but doesn't seem to work

        var mySkills = '';
        for (i=0, i < myInfo.skills.length, i++) {
          mySkills += 'li' + myInfo.skills + 'li';
        }
        document.write(mySkills);

Any help will be appreciated. Maybe I went wrong somewhere in the code.

  • Three errors. Firstly for uses `;` in between the options instead of `,`. Second, you're not using the i, so isntead of `myInfo.skills` should be `myInfo.skills[i]`. Thirdly, if you want to use strings to create new `li` do `<'li>' + myInfo.skills + '' – A. L Mar 22 '17 at 04:11
  • `var mySkills = myInfo.skills.map(skill => 'li' + skill + 'li')` will produce: ["liPHPli","liJavali"...]. _Since this was marked as duplicate, I couldn't post an answer. Also, note that "[Arrow Functions](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions)," the `(skill) => 'li' + skill + 'li'` part (short for `function(skill){ return 'li'+skill+'li' }`) won't work in IE (of course), but will work in Edge, for what it's worth._ – Travis Heeter Apr 19 '17 at 13:31

4 Answers4

2

I would write a routine which creates an li:

function li(content) {
  const elt = document.createElement('li');
  elt.innerHTML = content;
  return elt;
}

Now, create a document fragment to put all those lis into, based on an array:

function makeLis(contents) {
  const frag = document.createDocumentFragment();

  contents.forEach(content => frag.appendChild(li(content));
  return frag;
}

Write a function to create a ul element to put the li's into:

function makeUl(contents) {
  const ul = document.createElement('ul');

  ul.appendChild(makeLis(contents));
  return ul;
}

Now you can write that into your document:

document.getElementById('target').appendChild(makeUl(contents));
1

get the values of skills through index

var mySkills = '';
for(i=0; i < myInfo.skills.length; i++) {
    mySkills += '<li>' + myInfo.skills[i] + '</li>';
}
document.write(mySkills);
Chinito
  • 1,085
  • 1
  • 9
  • 11
1

You can access an individual object in the Object Array through index []. The objects are 0 index based.

skills: ['PHP', 'Java', 'JavaScript', 'HTML', 'CSS', 'jQuery'],

PHP is the start of the object array, at position 0

mySkills += '<li>' + myInfo.skills[i] + '</li>';

i will increment for every pass until,

for(i=0; i < myInfo.skills.length, i++) {

i is greater then the length of the array, 0,1,2,3,4,5.

a semi-colon is required when initializing the variable.

Ryan Dooley
  • 224
  • 1
  • 3
  • 16
  • [Repeating same mistake](https://stackoverflow.com/questions/42942437/looping-array-which-is-inside-an-object#comment72981483_42942454) – Tushar Mar 22 '17 at 04:03
0

Unless you need to access the index as well, you can use for.. of

var myInfo = {
            skills: ['PHP', 'Java', 'JavaScript', 'HTML', 'CSS', 'jQuery'],
            languages: ['English', 'German']
            }
            
for (let skill of myInfo.skills)
{
  console.log(skill);
}

If you do need to access the index and you don't want to write a long i=0; i < length; i++ you can use for... in instead

var myInfo = {
            skills: ['PHP', 'Java', 'JavaScript', 'HTML', 'CSS', 'jQuery'],
            languages: ['English', 'German']
            }
            
for (let index in myInfo.skills)
{
  console.log(myInfo.skills[index], "at index", index);
}
A. L
  • 11,695
  • 23
  • 85
  • 163
  • @KautilyaKondragunta Because it's an li. It will default to dots unless you apply a css to them, see: http://stackoverflow.com/questions/1027354/need-an-unordered-list-without-any-bullets Also, why use `li` if you don't want the dotting? – A. L Mar 22 '17 at 04:45