gpt4 book ai didi

javascript - 遍历一个javascript数组将值添加到另一个数组

转载 作者:行者123 更新时间:2023-11-29 23:06:43 25 4
gpt4 key购买 nike

如何遍历这个数组,我想循环并将名称值添加到一个空数组,当我控制台 .log 它给出下面的结果时,你能帮忙吗,我的代码的结果是空数组

this.props.names it shows

(3) [{…}, {…}, {…}] 0: {name: "abc", id: 1} 1: {name: "def", id: 2} 2: {name: "gh", id: 3} length: 3 proto: Array(0)

let titles = []
let cats = []
cats = this.props.names
let len = this.props.names.length;
for( i = 0 ;i< cats.length ; i++){
titles.push(cats[i].id)
}

return titles;

最佳答案

据我所见...做

let titles = []
let cats = [{name: "abc", id: 1},{name: "def", id: 2},{name: "gh", id: 3}]
for( var p = 0 ;p< cats.length ; p++){
titles.push(cats[p].id)
}

console.log(titles)

关于javascript - 遍历一个javascript数组将值添加到另一个数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54595485/

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