制作一个姓名字符串数组,说明他们是否可以去黑客帝国(18岁以下)

问题描述 投票:0回答:1
function makeStrings(arr){
  // your code here
}

console.log(makeStrings([
    {
        name: "Angelina Jolie",
        age: 80
    },
    {
        name: "Eric Jones",
        age: 2
    },
    {
        name: "Paris Hilton",
        age: 5
    },
    {
        name: "Kayne West",
        age: 16
    },
    {
        name: "Bob Ziroll",
        age: 100
    }
])); 
// ["Angelina Jolie can go to The Matrix", 
// "Eric Jones is under age!!", 
// "Paris Hilton is under age!!", 
// "Kayne West is under age!!", 
// "Bob Ziroll can go to The Matrix"]`

我的代码是:

function makeStrings(arr){
    return arr.map((x) => x.age < 18) ? arr.map((x) => x.name + " can go to The Matrix ") :       arr.map((x) => x.name + " is underage!!"); 
}

但这又回来了

[
    "Angelina Jolie can go to The Matrix ",
    "Eric Jones can go to The Matrix ",
    "Paris Hilton can go to The Matrix ",
    "Kayne West can go to The Matrix ",
    "Bob Ziroll can go to The Matrix "
]
javascript arrays
1个回答
0
投票

当您从三元运算符 (x.age

然后使用类似的结构< 18), you can return 2 strings, one for true and one for false.

map()

我们首先添加名称和所需的后缀。

arr.map((x) => x.name + ((condition) ? 'true string' : 'false string'))

© www.soinside.com 2019 - 2024. All rights reserved.