我想以ES6方式从对象数组中获取数据。
我有数组对象。
const data = [
{
title: 'first',
description: "some desc",
url: firstUrl,
},
{
title: 'second',
description: "some desc",
url: secondUrl,
},
];
我正在尝试检查是否存在title === 'second'
,然后返回它的网址,否则返回firstUrl
我知道我可以使用for循环来做到这一点。但是,我想用ES6方式。任何帮助将不胜感激。
我尝试过:
let text = data.map((val) => { val.title === 'second' ? val.url : val.url });
但是不工作,我知道这不是好方法。
const secondItem = data.find(item => item.title ==='second'))const url = secondItem吗? secondItem.url || firstUrl
只需保持简单,像这样