如何从对象数组中提取对象(数组)?
How do I extract an object(array) from an array of objects?
假设我有以下数据集:
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'}
]},
]};
我想从 "spaces" 属性 中提取一个新对象(数组)。除此之外,我需要识别那些没有 "spaces" 属性 的 "title" 对象与空值相关联。
我想以这样的方式结束:
newArray = ['', '1', '', '', '2']
这可能吗?如果我的术语不是很好,请原谅我。新手。
你可以这样做
const newArr = art.fields.map(space => {
return {...space.text}
});
所以我假设您想 return 一个包含文本数组对象的新数组
应该不会太难。尝试 Javascript 的 map()
函数...
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 returns a function's result for an iteration over an array. Check out the Mozilla Developer Network Docs 上的代码。
您可以使用 map
方法为 art.fields
数组中的每一项计算一个值。
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'}
]},
]};
var newArray = art.fields.map(function(o){
return 'text' in o ? o.text[0].spaces : '';
});
console.log(newArray);
如果您的文本数组将有多个空格,那么它应该遍历它并添加空格值,如下所示:
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'
}]
},
]
};
var res = art.fields.reduce((ini, curr, idx) => {
if (curr.text) {
curr.text.forEach(arr => {
arr.spaces && ini.push(arr.spaces);
});
} else {
ini.push('');
}
return ini;
}, [])
console.log(res);
假设我有以下数据集:
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'}
]},
]};
我想从 "spaces" 属性 中提取一个新对象(数组)。除此之外,我需要识别那些没有 "spaces" 属性 的 "title" 对象与空值相关联。
我想以这样的方式结束:
newArray = ['', '1', '', '', '2']
这可能吗?如果我的术语不是很好,请原谅我。新手。
你可以这样做
const newArr = art.fields.map(space => {
return {...space.text}
});
所以我假设您想 return 一个包含文本数组对象的新数组
应该不会太难。尝试 Javascript 的 map()
函数...
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 returns a function's result for an iteration over an array. Check out the Mozilla Developer Network Docs 上的代码。
您可以使用 map
方法为 art.fields
数组中的每一项计算一个值。
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'}
]},
]};
var newArray = art.fields.map(function(o){
return 'text' in o ? o.text[0].spaces : '';
});
console.log(newArray);
如果您的文本数组将有多个空格,那么它应该遍历它并添加空格值,如下所示:
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'
}]
},
]
};
var res = art.fields.reduce((ini, curr, idx) => {
if (curr.text) {
curr.text.forEach(arr => {
arr.spaces && ini.push(arr.spaces);
});
} else {
ini.push('');
}
return ini;
}, [])
console.log(res);