gpt4 book ai didi

javascript - 将字符串值存储到循环内的数组

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:11:12 27 4
gpt4 key购买 nike

我想在循环中将 text 的所有值存储在一个数组中,这可能吗?有人可以提示我如何执行此操作吗?

  $('#wa li').each(function (i) {

var text = $(this).text();

});

最佳答案

只需使用 .map.get – 不需要中间变量或循环

const liTexts = 
$ ('#wa li')
.map ((idx,elem) => $(elem).text ())
.get ()

console.log (liTexts)
// [ 'one', 'two', 'three' ]
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="wa">
<li>one</li>
<li>two</li>
<li>three</li>
</ul>

关于javascript - 将字符串值存储到循环内的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841348/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com