0

I have sent some data to my server, and I want to be able to pull the data from the object to use for my server. An example of what the data structure is down below:

[
  [
    {
      path: 'path/image',
      preview: 'blob:imagepreview'
    }
  ]
]

I wouldn't be able to use dot notation. nor a single bracket notation would work.

Edit:

A double bracket notation like array[0][0] worked for me. I was able to pull out the information and access it like normal.

graysonmcm
  • 87
  • 2
  • 17

3 Answers3

1

If the index is static you can access it easily like so:

var array = [[{path: 'path/image', preview: 'blob:imagepreview'}]]
console.log(array[0][0])  // { path: 'path/image', preview: 'blob:imagepreview' }

From there you can use both dot and bracket notations.

SCL
  • 86
  • 5
0

so assuming you have your data in a variable called data, you could do this to access the path and preview directly

const [{path, preview}] = data.flat()

or if you wanna extract the item as a whole

const [item] = data.flat()
Haythem Farhat
  • 737
  • 7
  • 13
0

If specific structure is unknown this recursive function will store all objects to result variable, assuming no repeat keys(code can be tweaked to overcome).

const data = [
  [{
    path: 'path/image',
    preview: 'blob:imagepreview'
  }]
]
console.log(data)
let result = {}
let getValues = (elem) => {
  if (Array.isArray(elem)) {
    elem.map(e => getValues(e))
  } else if (elem instanceof Object) {
    for (const key in elem) {
      if (Array.isArray(elem[key])) {
        getValues(elem)
      } else {
        result[key] = elem[key]
      }
    }
  }
}

getValues(data)
console.log(result)