[英]Can I declare an array of values that are nested inside a collection of objects without writing a loop?
Maybe a spread operator or destructuring instead? 也许是散布经营者还是改制? Instead of writing this function and calling it with the collection to create an array 而不是编写此函数并与集合一起调用以创建数组
function namesArray(group) {
let names = [];
group.values.forEach( (value) => {
names.push(value.name);
});
return names;
}
names: Array<string> = namesArray(group);
I want to do something like this: 我想做这样的事情:
names: Array<string> = group.values[....].value.name;
with the ellipsis representing some kind of operator. 省略号代表某种运算符。
您正在寻找map()
:
const names = group.values.map(v => v.name);
Use the combination of map
and arrow functions 结合使用map
和箭头功能
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Functions/Arrow_functions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.