最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

print Associative array elements one by one using javascript only - Stack Overflow

programmeradmin5浏览0评论

I have this code

<script type="text/javascript">
var slideStart = 1;
var slideCount = 4;
function callArr() {
    var arrName = {"rose":"5", "daisy":"4",
                   "orchid":"3", "sunFlower":"10",
                   "Lily":"15"};
    for (var flwr in arrName) {
        if (slideStart <= slideCount) {
            document.getElementById('test').innerHTML += flwr +
                                                         "   >>>>>>>>   " +
                                                         arrName[flwr] +
                                                         "<br />";
            slideStart++;
            break;
        }
    }
}
</script>

I want to access array elements individually on any event. can any please help me out on this????

I have this code

<script type="text/javascript">
var slideStart = 1;
var slideCount = 4;
function callArr() {
    var arrName = {"rose":"5", "daisy":"4",
                   "orchid":"3", "sunFlower":"10",
                   "Lily":"15"};
    for (var flwr in arrName) {
        if (slideStart <= slideCount) {
            document.getElementById('test').innerHTML += flwr +
                                                         "   >>>>>>>>   " +
                                                         arrName[flwr] +
                                                         "<br />";
            slideStart++;
            break;
        }
    }
}
</script>

I want to access array elements individually on any event. can any please help me out on this????

Share Improve this question edited Jul 5, 2011 at 7:27 6502 115k16 gold badges174 silver badges275 bronze badges asked Jul 5, 2011 at 7:22 MahimaMahima 5091 gold badge6 silver badges18 bronze badges 6
  • You have the code to print the array. Now, what do you want to know? – GolezTrol Commented Jul 5, 2011 at 7:23
  • What exactly do you mean by "on any event"? – jerluc Commented Jul 5, 2011 at 7:24
  • Just FYI, javascript doesn't have associative arrays. What you're working with there is actually an object. Treating them as associative arrays can have unintended consequences. ajaxian./archives/… – GordonM Commented Jul 5, 2011 at 7:25
  • this code print only first element... i want all elements to print one by one when i click on a link.. Is it possible?? – Mahima Commented Jul 5, 2011 at 7:25
  • Maybe you should remove the if from your code? And especially, don't use > in HTML, unless you mean to use a tag. – GolezTrol Commented Jul 5, 2011 at 7:30
 |  Show 1 more ment

5 Answers 5

Reset to default 2

Of course it will print only the first element since you use condition like this

if (slideStart <= slideCount)

Modify your code to iterate through the object properties. One of the possible solutions:

var slideStart = 0;
var slideCount = 4;
function callArr() {
    var arrName = {"rose":"5", "daisy":"4",
                   "orchid":"3", "sunFlower":"10",
                   "Lily":"15"};
    var i = 0;
    for (var flwr in arrName) {
        if (i == slideStart) {
            document.getElementById('test').innerHTML += flwr +
                                                         "    " +
                                                         arrName[flwr] +
                                                         "<br />";
            slideStart++;
            break;
        }

        i++;
    }
}

Try it here: http://jsfiddle/2nDv8/

As you can see, it prints the next property after every call.

If you refer to access anywere, you could declare it as globla var:

<script type="text/javascript">
var slideStart = 1;
var slideCount = 4;
var arrName = {"rose":"5", "daisy":"4",
                   "orchid":"3", "sunFlower":"10",
                   "Lily":"15"}; //Declare var outside, so it's global
function callArr() {    
    for (var flwr in arrName) {
        if (slideStart <= slideCount) {
            document.getElementById('test').innerHTML += flwr +
                                                         "   >>>>>>>>   " +
                                                         arrName[flwr] +
                                                         "<br />";
            slideStart++;
            //break; //This prevents from showing more. Erase it
        }
    }
}
</script>

Also note that the break statement was causing to show only the first element

Hope this helps. Cheers

This isn't exactly cross-browser patible, but you can use Object.keys(arrName). Or actually create and store an array that acts the same, if you're actually concerned about cross-browser patibility.

It only prints once because you put a "break" inside your loop. This code will print all elements without problems. One problem is also caused by your slideStart and slideCount variables. If you want to print all of them, change slideStart to 0 so that it will print all the elements, not just 4 of those..Hope that helps:

function callArr() {
    var arrName = {"rose":"5", "daisy":"4",
                   "orchid":"3", "sunFlower":"10",
                   "Lily":"15"};
    for (var flwr in arrName) {
        if (slideStart <= slideCount) {
            document.getElementById('test').innerHTML += flwr +
                                                         "   >>>>>>>>   " +
                                                         arrName[flwr] +
                                                         "<br />";
            slideStart++;
            //break;
        }
    }
}

Probably this is what you want. Try it here: http://jsfiddle/Hsn4c/1/

function callArr() {
    var slideStart = 0;
    var slideCount = 4;

    var arrName = {
        "rose": "5",
        "daisy": "4",
        "orchid": "3",
        "sunFlower": "10",
        "Lily": "15"
    };
    for (var flwr in arrName) {
        if (slideStart <= slideCount) {
            document.getElementById('test').innerHTML += flwr + ">>>>>" + arrName[flwr] + "<br />";
            slideStart++;
            //break;
        }
    }
    document.getElementById('test').innerHTML += '==========<br />';
}
发布评论

评论列表(0)

  1. 暂无评论