简体   繁体   English

是否可以声明嵌套在对象集合内的值数组而无需编写循环?

[英]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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在从 html 集合创建的数组中通过 for 循环从对象中获取元素的值? - How can I get values of elements from objects through for loop in an array created from a html collection? 我可以在对象数组中重复n次而不使用javascript中的循环吗? - Can I repeat the values in an array of objects n times without using loop in javascript? 访问嵌套在数组内的对象的特定值 - accessing specific values of objects nested inside an array 如何将嵌套对象的值分组到数组中? - how can i group values of nested objects into array? 如果我使用传播语法,如何完全返回内部嵌套对象的数组? - How can I completely return the array with nested array of objects inside if I am using spread syntax? 为什么不能在for循环中声明变量? - Why can't I declare a variable inside a for loop? 为什么我不能在 for 循环中声明 ```var highScore = 0;``` - Why i can't declare ```var highScore = 0;``` inside for loop 如何在 TypeScript 中声明具有嵌套对象数组的 object? - How to declare an object with nested array of objects in TypeScript? 如何在Typescript中为对象数组声明数据类型? - How can I declare a datatype for an array of objects in Typescript? 如何循环数组并将对象数组中的值输出为“值”和“标签”? - How to loop array and output the values inside an array of objects as 'value' and 'label'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM