简体   繁体   English

如何循环遍历 object 数组并从数组中列出的两个对象中获取一个属性元素?

[英]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作为键而不是title1title2

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.

相关问题 如何提取对象数组中每个对象的两个元素并制作一个新元素 - How can I pull out two elements of every object in an array of objects and make a new one 如何对未排序的对象数组的 OBJECT 中的元素重新排序? - How can I reorder an ELEMENT inside of an OBJECT of an array of OBJECTS NOT sort? 如何遍历数组对象并检查每个元素以查看是否已定义? - How can I loop through an array object and check each element to see if one is defined? 我如何才能仅获得保留一个属性的两个数组对象之间的差异? - How i can get only the difference between two array objects keeping one property? 如何循环遍历 object 并获取每个元素的索引并将其存储在数组中? - How can I loop through an object and get the index of each element and store it in an array? 如何获取 object 中数组元素的索引? - How can I get the index of array element inside object? 用对象在数组内部循环以获取其中一个属性 - Looping inside of array with objects to get one of it property 获取对象数组内对象属性的值 - Get value of object property inside array of objects 如何访问对象数组中的两个单独的图像,并将其分配给同一数组中的第三个对象? - How can I access two separate images in an array of objects and assign it to a third object inside the same array? 当对象在数组中时如何获取一个对象属性? - How to get one object property when the object is inside a array?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM