-2

I want to get all name from the array of data. Is there any way to do it without using an iterator?

const data = [
    {name: 'Rushabh', age: 22},
    {name: 'Bonny', age: 24}
]

console.log(Object.values(data));
Rushabh Shah
  • 133
  • 1
  • 4
  • 1
    you can always do `data.map(obj => obj.name)` Not sure whether this counts as "using an iterator" or not though. (Any array is technically an iterator.) – Robin Zigmond Mar 07 '19 at 12:24
  • I know but is there any default method to do this thing? – Rushabh Shah Mar 07 '19 at 12:25
  • please add the wanted result. – Nina Scholz Mar 07 '19 at 12:26
  • 1
    Possible duplicate of [How to get all properties values of a Javascript Object (without knowing the keys)?](https://stackoverflow.com/questions/7306669/how-to-get-all-properties-values-of-a-javascript-object-without-knowing-the-key) – Nikola Lukic Mar 07 '19 at 12:28
  • not to be picky, but the question, whether or not it makes sense, was to **not** use an iterator. **Why?** doesn't matter at this point. Well, without **iterating** you would need to know how many items are in the array before hand, since you, again, are not **iterating**. Therefore, ```data[0].name``` and ```data[1].name``` are your two choices here. There have been solutions propsoed, but they **all** use **iteration**. yes, **map** is an iteration – gkelly Mar 07 '19 at 12:46
  • Possible duplicate of [From an array of objects, extract value of a property as array](https://stackoverflow.com/questions/19590865/from-an-array-of-objects-extract-value-of-a-property-as-array) and [Get array of property values from array of objects with jquery](https://stackoverflow.com/questions/29472655) – adiga Mar 07 '19 at 12:51

5 Answers5

1

For getting only a single property, you need to map this property directly.

const
    data = [{ name: 'Rushabh', age: 22 }, { name: 'Bonny', age: 24 }],
    result = data.map(({ name }) => name); // get only name

console.log(result);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Try this:

const data = [
    {name: 'Rushabh', age: 22},
    {name: 'Bonny', age: 24}
]
const names = data.map(({name}) => name)
console.log(names);

the names will include the list of names.

Nisarg Shah
  • 14,151
  • 6
  • 34
  • 55
0

Without some kind of iterator you can not do that. You can use map() with short hand property.

const data = [
    {name: 'Rushabh', age: 22},
    {name: 'Bonny', age: 24}
]
const name = data.map(({name}) => name);
console.log(name);
Mamun
  • 66,969
  • 9
  • 47
  • 59
0

Use Array.map() and extract the name property:

const data = [
    {name: 'Rushabh', age: 22},
    {name: 'Bonny', age: 24}
]

console.log(data.map(x => x.name));
jo_va
  • 13,504
  • 3
  • 23
  • 47
0

If you are using JQuery, do this:

const data = [
    {name: 'Rushabh', age: 22},
    {name: 'Bonny', age: 24}
] 
var names = $.map( data, function(item, key) { return item.name; } );
// names : ['Rushabh', 'Bonny']
Sahith Vibudhi
  • 4,935
  • 2
  • 32
  • 34