在 for 循环中使用 jQuery.each 将数组映射到元素文本?

use*_*751 0 javascript arrays jquery loops

对于给定的 html:

<div id="parent">
    <div class="child">x</div>
    <div class="child">y</div>
    <div class="child">z</div>
</div>
Run Code Online (Sandbox Code Playgroud)

如何从数组中映射其子级的文本?

这是我的尝试:

var myArray = ['a', 'b', 'c'];

for (var i = -1; i < myArray.length; i++) {
    $('#parent .child').each(function () {
        $(this).text(myArray[i]);
    });
}
Run Code Online (Sandbox Code Playgroud)

我得到:c,c,c

我怎样才能得到a,b,c

flo*_*sse 5

您不需要外for循环:

var myArray = ['a', 'b', 'c'];

$('#parent .child').each(function (index) {
    $(this).text(myArray[index]);
});
Run Code Online (Sandbox Code Playgroud)