-2

I am logging namesDefinitions.name to the console log and not to sure why it is turning back as undefined in the console log. it shows (14 undefined) in the console. Shouldn't this show me the list of all the names?

 const unitTests = unitTestTemplates.map((item) => {

    const namesDefintions = [
      {name: 'REASONABLENESS', definition: 'Verifies that the data aligns to operational context. E.g. a birth date of 01/01/01 is valid, but generally not reasonable'},
      {name: 'COMPLETENESS', definition: 'Verifies that all necessary data is present in a dataset.'},
      {name: 'VALIDITY', definition: 'Verifies that the data conforms to the format of its definition'},
      {name: 'UNIQUENESS', definition: 'Verifies that an attribute is unique and free of duplication '},
      {name: 'TIMELINESS', definition: 'Verifies that data is up to date and available for use as required by the busines'},
      {name: 'CONSISTENCY', definition: 'Verifies that the data is free of contradictions. Especially where data is pulled from multiple environments'},
      {name: 'INTEGRITY', definition: 'Verifies that data is accurate and consistent across its lifecycle. Each time data is replicated or transferred, it should remain intact and unaltered between updates. Error checking methods should be used to ensure that the data is transferred without alteration.'},
      {name: 'RECONCILIATION', definition: 'Verifies the accuracy of financial attributes and measures as they are transformed through the various layers of the data platform. This can take the form of an aggregated sum comparison which prevents any mismatch caused by changing the granularity of qualifying dimensions.'}
    ]
       console.log(namesDefintions.name);

    

    })
Emile Bergeron
  • 17,074
  • 5
  • 83
  • 129
bloom bloom
  • 131
  • 8
  • `namesDefintions` is an array, so it won't have a `name` property, thus that will yield `undefined` when logging. I can only assume then that `unitTestTemplates` has 14 elements in it. What are you really trying to do? – Drew Reese Sep 17 '20 at 05:18
  • I am trying to access the name property from the namesDefinition array* – bloom bloom Sep 17 '20 at 05:20
  • The array won't have a `name` property. Are you wanting to iterate the array and access the `name` property of each element? – Drew Reese Sep 17 '20 at 05:21
  • yes that is correct. – bloom bloom Sep 17 '20 at 05:27
  • 1
    Then some form of `namesDefintions.forEach(({ name }) => console.log(name))` to iterate over the data, get the `name` property, and log it as lanxion has answered. Side note, array::map should have zero side-effect, like console logging, and should return a value for the new array it maps to. – Drew Reese Sep 17 '20 at 05:30

2 Answers2

1

You are not looping over an array, you are just trying to access the name property of namesDefinition array, without specifying the index first. You first need to actually loop through the array using either forEach, a for loop or a while loop. Here is an example with a forEach:

namesDefinition.forEach(nd => {
  console.log(nd.name);
})

NOTE: namesDefinition won't have a name property, but namesDefinition[0...length-1] would, you need to clear up some basics of arrays and objects from what I can see.

lanxion
  • 1,350
  • 1
  • 7
  • 20
0

You are trying to console.log namesDefintions.name and the namesDefintions is an array of objects. In react if you want to do that you will have to use map function

In react native you can use both Map function or import {FlatList} from 'react-native' library and make use of FlatList in your code. (go through the documentation if needed)

You need to use the map function on the 'array of objects' you want to print.

So try this below code

namesDefintions.map((item) => {
  console.log(item.name)
})
adiga
  • 34,372
  • 9
  • 61
  • 83
Dhruv
  • 30
  • 5
  • array::map is for mapping the elements of one array to a new array, with no side-effects. Use array::forEach to iterate over an array and invoke a function to issue side-effects, like logging or any other external calls. – Drew Reese Sep 17 '20 at 05:43
  • `console.log(namesDefintions.name)` should be `console.log(item.name)` – adiga Sep 17 '20 at 05:48