Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extracting properties from NSArray of objects

Tags:

Is there any of way (other than looping) of extracting a particular property of all objects in an array. So say there in an array of people. I want to extract all their first names into an array.

like image 403
VBK Avatar asked Sep 07 '12 05:09

VBK


People also ask

How do you access data from an array of objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

How do you extract an element in JavaScript?

To extract part of an array with JavaScript, we can use the JavaScript array slice method with the start and end index of the array to extract. We call slice on oldArray with the start and end indexes respectively. The end index is excluded from the returned array.

How do you Destructure an array of objects?

To destructure an array in JavaScript, we use the square brackets [] to store the variable name which will be assigned to the name of the array storing the element.


1 Answers

Key Value coding will help you with that:

NSArray *result = [people valueForKey:@"firstname"]; 
like image 94
JustSid Avatar answered Sep 20 '22 05:09

JustSid