1

I wanted to optimize my code removing a for-loop.

    groupFieldNames = [];
    for (i = 0; i < data.length; i++) {
        groupFieldNames.push(data[i].groupFieldName);
    }

data is an array of objects, each having 4 fields.

enter image description here

I am interested in the one identified as groupFieldName.

Is there a way to avoid the loop and directly push the fields in the array?

EDIT:

I went with @Yosvel Quintero suggestion (to all the guys suggesting the map solution, he was the first one), and checked the performance. With a data array having ~60k objects I've got:

  • 3ms using map;
  • 11ms using for-loop

Not bad.

4
  • Array.prototype.map() Commented Feb 27, 2019 at 9:52
  • You can use .map instead, but if you're looking for optimal efficient code, not much can beat a for loop... Commented Feb 27, 2019 at 9:52
  • You can use map, or forEach, whatever, it will iterate, just the loop won't be in your code, but you cannot avoid it. Commented Feb 27, 2019 at 9:53
  • Don't know your requirement but maybe you can keep the complete object and wherever you need groupFieldName, simply use object destructure {groupFieldName} = obj; or in methods methodName({groupFieldName}){//} Commented Feb 27, 2019 at 10:03

4 Answers 4

2

You can use Array.prototype.map()

const groupFieldNames = [];
for (i = 0; i < data.length; i++) {
    groupFieldNames.push(data[i].groupFieldName);
}

To:

const groupFieldNames = data.map(o => o.groupFieldName);
Sign up to request clarification or add additional context in comments.

1 Comment

As @sjahan pointed out, this still counts a loop, even if you cannot see it. I was looking for something like mem-copy or stuff like that. But i see the elegance in what you proposed.
1

You could effectively use Array's built-in .map() here as follows -

var data = [
	{ id: 1, groupFieldName: 'abcd' },
	{ id: 2, groupFieldName: 'pars' }
];
var groupFieldNames = data.map(obj => obj.groupFieldName)
console.log(groupFieldNames);

Comments

1

You can use map

var groupFieldNames = [];
var data=[{groupFieldName:'a'},{groupFieldName:'b'},{groupFieldName:'c'}]
  console.log(data.map(x=>x.groupFieldName))

You can also use forEach

var groupFieldNames = [];
var data=[{groupFieldName:'a'},{groupFieldName:'b'},{groupFieldName:'c'}]
data.forEach(x=>groupFieldNames.push(x.groupFieldName))
  console.log(groupFieldNames)

Comments

1

You can use Array.prototype.map() with Destructuring assignment in the following way:

const data = [
  {id: 66, groupFieldName: 'test', other: 'other'},
  {id: 66, groupFieldName: 'test2', other: 'other2'}
];

const groupFieldNames = data.map(({groupFieldName}) => groupFieldName);
console.log(groupFieldNames);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.