[英]How can I loop through an array of object and get one property element out of the two objects listed inside the array?
I have my array of object bellow.我有我的 object 波纹管数组。
const Articles = [
{
id: 1,
article1: 1,
title1:'Trust',
name1:'Pericles',
date1:'Dec 2, 2022',
text1: 'Lorem ipsum dolor sit consectetur. minima in quae dolores quis fugit officia, quia at quam ipsum iste suscipit, eum, veniam eaque voluptas?',
},
{
id: 2,
article2: 2,
title2:'Love',
name2:'Billey',
date2:'Dec 2, 2022',
text2: 'minima in quae dolores quis fugit officia, quia at quam ipsum iste suscipit, eum, veniam eaque voluptas?',
}
]
I have a function and I am trying to select Articles.title1 through mapping我有一个 function,我正在尝试通过映射到 select Articles.title1
function arc(){
Articles.map((element, index)=>{
console.log(element.title1)
})
I get "Trust"
for the title1
and that's all I needed but, I'm getting undefined
for the other.我得到了
title1
的"Trust"
,这就是我所需要的,但是,我对另一个undefined
。 Is there a way I can have just title1
?有没有办法让我只有
title1
? Thank you.谢谢你。
I have also tried for loop:我也尝试过循环:
for(let i = 0; i < Articles.length; i++){
console.log(Articles[i].title1)
}
and get the same result.并得到相同的结果。
Use title
as the key instead of title1
and title2
.使用
title
作为键而不是title1
和title2
。
And for other keys, you should do the same:对于其他键,您也应该这样做:
{
id: 1,
article: 1,
title:'Trust',
name:'Pericles',
date:'Dec 2, 2022',
text: 'Lorem ipsum dolor sit consectetur. minima in quae dolores quis fugit officia, quia at quam ipsum iste suscipit, eum, veniam eaque voluptas?',
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.