-1

I am trying to access all 22 of the timestamp values within the createdIssuesLength state variable...I am not to sure how to access these correctly as these have numbers before the object.

enter image description here

const [createdIssueslength, setCreatedIssueslength] = useState(null)

useEffect(() => {
  async function onLoadDefectLoggingData() {
    loading.setLoading(true)
    const results = await get(`get_defect_logging_report_data`, user.user)
    if (results.status === 0) {
   
      setCreatedIssueslength(results.data.created_resolved_chart)

    } else if (results.status >=20 && results.status <=30){
      snackbar.statusCheck(results)
      user.setSessionTokenMatches(false)
    } else snackbar.statusCheck(results)
    loading.setLoading(false)
    }
    onLoadDefectLoggingData()

}, [])
stack
  • 15
  • 4
  • Does this answer your question? [How can I access and process nested objects, arrays or JSON?](https://stackoverflow.com/questions/11922383/how-can-i-access-and-process-nested-objects-arrays-or-json) – CertainPerformance Oct 23 '20 at 00:57
  • Well, it is an array, are you trying to iterate the result object? – Drew Reese Oct 23 '20 at 00:58
  • yep that is correct :) – stack Oct 23 '20 at 00:58
  • I want to be able to access all 22 of the values from create_timestamp – stack Oct 23 '20 at 01:00
  • @CertainPerformance I want to access all 22 of the values not just one. – stack Oct 23 '20 at 01:23
  • Iterating arrays if a fairly trivial matter in almost any language. Javascript has probably a dozen or so. Accessing object properties is also a trivial matter. What in your snippet is the array from the console log output above it? What are you trying to do? From the array what is your expected result from iteration? What have you tried that isn't working? – Drew Reese Oct 23 '20 at 02:33

1 Answers1

0

You are receiving an array. Basically the way to access it is using an array method like .forEach or .map

Your hook useEffect it's probably throwing an error because you are trying to access to status property of the received array (which, of course, it does not exists); for a better understanding of this, imagine you have the variable const myArr = [] and you are trying to do myArr.status, javascript will complain because .status it's not a propoerty from the Array object

In order to iterate through every element of the array received from the function await get('get_defect_logging_report_data', user.user) you'd need to do something like:

const results = await get('get_defect_logging_report_data')
// let's iterate over the array
results.map(result => {
  // here you will be able to access to your objects, something like:
  console.log(result.status) // This will show "Backlog" or "In progress"
  console.log(result.create_timestamp) // This will show your timestamp
})

Inside of every iteration you can do whatever logic you'd like to do

Cesar
  • 417
  • 5
  • 17