0

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.

Nick
  • 73
  • 6
  • If you're going to operate on an array, some function is going to iterate over the array. That can either be code you manually write or built-in functions that know how to iterate over an array (`.map()`, `.forEach()`, `.reduce()`, etc...). There's no magic here. – jfriend00 Dec 11 '16 at 03:46
  • @jfriend00 _"without writing a loop?"_ – guest271314 Dec 11 '16 at 03:47
  • @guest271314 - The point of my comment is that there's no way to operate on an entire array without some piece of code iterating over the array in a loop whether it's your code or a function that has the loop built-in. An Array is not an intrinsic type in any typical PC CPU so to operate on the entire array a loop is required somewhere. – jfriend00 Dec 11 '16 at 03:53
  • @jfriend00 _"The point of my comment is that there's no way to operate on an entire array without some piece of code iterating over the array in a loop"_ Yes, it is possible, see http://stackoverflow.com/a/40078065/, at least where the `.length` of the array is known. – guest271314 Dec 11 '16 at 03:54
  • @guest271314 - And, you think a loop isn't involved? Get real. An array is not an intrinsic type of the CPUs we use and a Javascript array certainly isn't something a CPU operates on directly, so there's a loop involved no matter what. This is a bad question because it's unclear what the OP really means by without a loop. I'm bowing out of this pointlessly unclear discussion. – jfriend00 Dec 11 '16 at 03:57
  • @jfriend00 Interpreted the Question, here, as OP is asking how to perform the process without using `for`, `for..in`, `for..of`, `.map()`, `.forEach()` or other standard loop or iterators; using spread element, rest element and destructuring assignment. OP is already using `Array.prototype.forEach()`. – guest271314 Dec 11 '16 at 03:59
  • guys, I think the OP just tries to get rid of the `namesArray()`-function, hoping that there might be something simpler than `array.forEach()` + `results.push()`. imo the best way here is `names: Array = group.values.map(value => value.name)`, as [SLaks already posted in his answer](http://stackoverflow.com/a/41082663/6567275). short, simple, inline, readable, understandable – Thomas Dec 11 '16 at 04:24
  • 1
    @Thomas - If the OP was on the ball, then they would participate in this discussion, but an hour has passed and NO clarification has been provided to be sure what they mean. That's bad for stack overflow. Note to the OP. You should be checking back here several times in the first hour after you post to make sure your question was clearly understood. – jfriend00 Dec 11 '16 at 04:31
  • Possible duplicate of [Destructure object properties inside array for all elements](http://stackoverflow.com/questions/40069301/destructure-object-properties-inside-array-for-all-elements) – guest271314 Dec 11 '16 at 05:04
  • @Thomas thanks, yes, I'm to learn how to represent it concisely, inline, so that i can quickly and easily pass it into another function call as an argument without having to assign variables. That's why I was hoping there is an operator, or a concise way to use an operator. jfriend00 is correct in that there's a routine somewhere in there, but like so many other javascript operators and language constructs, it is baked into an operator or construct by compiler i.e. spread operator. So to be as clear as I can, I want to know how to access object props on the right hand side of a spread operator – Nick Dec 12 '16 at 16:25
  • @user2817034 _"So to be as clear as I can, I want to know how to access object props on the right hand side of a spread operator"_ Have you reviewed the answers at linked Question? – guest271314 Dec 12 '16 at 20:38

2 Answers2

3

You're looking for map():

const names = group.values.map(v => v.name);
4castle
  • 32,613
  • 11
  • 69
  • 106
SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 1
    _"without writing a loop?"_ – guest271314 Dec 11 '16 at 03:44
  • @guest271314 Technically this is not a loop, and neither is the code used by the OP. – 4castle Dec 11 '16 at 03:48
  • @4castle OP is asking how to perform the process using destructing assignment. OP is already using `Array.prototype.forEach()`. See http://stackoverflow.com/a/40078065/ – guest271314 Dec 11 '16 at 03:48
  • @4castle - It's using a function that contains a loop. There IS a loop. It's just inside the `.map()` function. The question is basically an unclear and bad question because it is not clear what they mean. – jfriend00 Dec 11 '16 at 03:55
  • @guest271314 The best you can do is `map(({name}) => name)` – 4castle Dec 11 '16 at 03:55
  • @4castle That is not the present Question. OP is asking how to, or if it is possible to, perform the process using destructuring assignment, spread element and, or rest element. – guest271314 Dec 11 '16 at 03:57
  • @jfriend00 The implementation could just as easily be recursion, but that's probably irrelevant. – 4castle Dec 11 '16 at 03:59
  • this is the best solution for me so far, although map is not an operator or some language construct, it is a native JS method belonging to Array, an extension of a native JS Object. I was hoping to use an operator somehow and access the iterated Object's property specifying on the right of the operator or something like that. – Nick Dec 12 '16 at 16:30
0

Use the combination of map and arrow functions

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

Rápli András
  • 3,869
  • 1
  • 35
  • 55