
JavaScript数组对象值循环提取方法
本文介绍如何使用JavaScript循环遍历数组对象并提取所需的值。
针对特定数据结构,您可以使用以下代码:
<code class="javascript">for (const key in arr) {
const countArray = [];
const yearArray = [];
for (const subkey in arr[key].lists) {
countArray.push(arr[key].lists[subkey].count);
yearArray.push(arr[key].lists[subkey].year);
}
console.log(countArray);
console.log(yearArray);
}</code>这段代码将输出以下结果:
立即学习“Java免费学习笔记(深入)”;
<code>['0', '3', '2', '1'] ['2020', '2021', '2022', '2023'] ['1', '2', '3', '4'] ['2020', '2021', '2022', '2023'] ['3', '2', '3', '4'] ['2020', '2021', '2022', '2023']</code>
选择for...in或for...of循环取决于您的数据结构。 根据具体情况,您可能需要调整代码以适应不同的数据结构。 例如,使用map()方法可以更简洁地实现相同的功能。
以上就是如何用JavaScript循环提取数组对象中的值?的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号