0

I have this array "Group":

enter image description here

i would like to have the value of name (test01) but i have undefined with console.log(group['name']), console.log(group[name]) or console.log(group.name).

How i can display the value ?

Thanks

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
Yobogs
  • 443
  • 1
  • 5
  • 17
  • It's an array of objects: group[1].name should work – gaetanoM Aug 07 '19 at 22:18
  • Do 'console.log(group)' just to make what group is – bhantol Aug 07 '19 at 22:19
  • 1
    you are using an array as if it's an object which isn't good practice since array.prototype contributes some properties. this can lead to bugs if you frequently use arrays incorrectly like this. Use an object and if it still doesn't work ask again. – Chris Rollins Aug 07 '19 at 22:19
  • Please post the actual output and the code you're working with instead of a picture of the console. Console emissions are implementation-dependent and often unclear. – ggorlen Aug 07 '19 at 22:26
  • I was wondering how that console output could happen. It shows an empty array but the expanded view shows properties. I'm guessing that this array is being mutated by asynchronous functions so it has these properties at certain times but does not have them at other times... – Chris Rollins Aug 07 '19 at 22:27
  • Good guess, but it seems unclear until the code and actual data structure shows up. It looks like Chrome, but usually when you set properties on an array you see them as key-value pairs like [this](https://stackoverflow.com/questions/56190829). – ggorlen Aug 07 '19 at 22:29
  • 1
    I have replicated this behavior in the console: https://puu.sh/E2vGp/ea79e4eafc.gif It's caused by mutating the array after displaying it, and then expanding it after mutating it. – Chris Rollins Aug 07 '19 at 22:35
  • Nice. Even so, OP accepted a solution that made an assumption about an object as a child of the array, so it's all a total mystery as to how OP actually came to get this screenshot. – ggorlen Aug 07 '19 at 22:45

2 Answers2

0

It's an array of objects - access the specific object first using group[0] then access the property you want with group[0].name.

const group = [{ name: "test01" }];

const res = group[0].name;

console.log(res);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

Based on what you have you can call your propriety if it's an object or an array of objects.

const object = {
  name: "test1",
  group: "c2"
}

console.log(object.name)

const array = [{
  name: "test1",
  group: "c2"
}]

console.log(array[0].name)
Aziz.G
  • 3,599
  • 2
  • 17
  • 35