javascript数组,对象,键:值

cbjzeqam  于 2022-11-20  发布在  Java
关注(0)|答案(3)|浏览(135)

选择数组中对象的所有值。例如,

tutorials = [{title:a, movie:a}, {title:b, movie:b}, {title:c, movie:c}] => a b c.

我想找出所有对象标题的值。
Tutorials[number].title,但无法获取所有对象的标题值。

mqkwyuun

mqkwyuun1#

您可以使用 for loop,以下是示例:

let tutorials = [
    {
        title:'a', movie:'a'
    },
    {
        title:'b', movie:'b'
    }, 
    {
        title:'c', movie:'c'
    }
]

for(let i = 0; i < tutorials.length; i++) {
    console.log(tutorials[i].title);
}
whitzsjs

whitzsjs2#

佐赫拉布的答案是正确的。我只是在展示另一种方式:

let tutorials = [
    {
        title:'a', movie:'a'
    },
    {
        title:'b', movie:'b'
    }, 
    {
        title:'c', movie:'c'
    }
]

const titles = [];
for (tutorial of tutorials) {
  titles.push(tutorial.title)
}
kuuvgm7e

kuuvgm7e3#

const titles = tutorials.map(t => t.title)

将在一条线上完成工作

相关问题