更新时间:2023-02-19 09:26:16
应该不会太难.试试 Javascript 的 map()
函数...
It shouldn't be too hard. Try Javascript's map()
function...
const art = {
'fields': [
{title:'Title 1'},
{'text': [
{spaces: '1'}, {link: 'This is a link'}, {mouse: 'Yes'}
]},
{title: 'Title 2'},
{title:'Title 3'},
{'text': [
{spaces: '2'}, {link: 'This is a different link'}, {mouse: 'No'}
]},
]};
const spaces = art.fields.map(function(field) {
if(field.text) {
return field.text[0].spaces;
}
return '';
});
console.log("Spaces?");
console.log(spaces);
结果...
"Spaces?"
["", "1", "", "", "2"]
查看用于 JSBIN 的代码.Map 返回一个函数对数组的迭代结果.查看 Mozilla 开发者网络文档 就可以了.
See the code working on JSBIN. Map returns a function's result for an iteration over an array. Check out the Mozilla Developer Network Docs on it.