1

I'm parsing a CSV with Papaparse, adding a new column of information in the CSV and creating a table with it. I have a method which retrieves the information that I want to add in the CSV and I need to push the return of this method in order to create the table with the new data. The problem is the i can see the returned value with console.log() but when I push this value into an array, nothing happens.

This is the code to parse the CSV and add the new column of data:

fileInput.addEventListener("change", event => {
  Papa.parse(fileInput.files[0], {
    skipEmptyLines: true,
    complete: results => {
      const data = results.data.slice(1)
      const headers = results.data[0]

      headers.push("Endereço")

      for (let item of data) {
        let itemLat = Number(item[0].split(".").join(""))
        let itemLon = Number(item[1].split(".").join(""))

        let coordinates = { lat: itemLat, lon: itemLon }

        let whereAreWe =
          places.places.filter(place => place.isHere(coordinates))


        console.log(whereAreWe)
        item.push(whereAreWe.toString())
      }

      tableCsv.update(data, headers)
    }
  })
})

And this is the method i'm using:

isHere(alertCoordinates = {}) {
  const latitude = alertCoordinates.lat
  const longitude = alertCoordinates.lon

  if (latitude > this.coordinates.botLeft.lat && latitude < this.coordinates.topLeft.lat) {
    if (longitude > this.coordinates.botLeft.lon && longitude < this.coordinates.botRight.lon) {

      return this.name
    }
  }

}

I tried to use JSON.stringify, toString(), bracket notation, dot notation, assing the return to a variable before pushing to the array, but nothing works. Sometimes is pushed a black string, sometimes the [object Object].

data.push works with anything except the return of my method.

pilchard
  • 12,414
  • 5
  • 11
  • 23
A. Curvina
  • 31
  • 3
  • 2
    You never `console.log(item)` – Quentin Jan 17 '21 at 21:53
  • Assuming that `places.places` is an array (otherwise `places.places.filter` will throw an exception) also `whereAreWe` is an array of places which contains 0 or more elements. Ff you call `toString()` on an empty array, the result is an empty string. If you call `toString()` on an array which contains one or more objects, the result is `[object Object]` so the behaviour you see is totally consistent with the JS standard – derpirscher Jan 17 '21 at 22:05
  • The real question is: What do you want `data.items` to be? An array of places? An array of array of places? A single place? That's not clear from the context. Depending on that, you must use a different accessor – derpirscher Jan 17 '21 at 22:09
  • Yes, you are right. the script is behaving as it should, but what do I do to retrieve the value of the method return? I need to put this value in the array. – A. Curvina Jan 17 '21 at 22:12
  • what "method return"? – derpirscher Jan 17 '21 at 22:14
  • places.places is an array of places. Every place has a location and a method isHere(). this method checks if a given location is inside the place a returns the place name. I need to append this value to the item array. – A. Curvina Jan 17 '21 at 22:16
  • You proably misunderstand what the callback of [`filter`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) does. It just decides whether the object you are calling it on should be part of the resulting array. It does not modify the object in any way. – derpirscher Jan 17 '21 at 22:17
  • 1
    `whereAreWe` is still an array of places and not an array of names or even a single name. You can use `whereAreWe.map(place => place.name)` to get an array of names. [`map`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/filter) And if you want a single name you can use `whereAreWe.map(place => place.name)[0]` to get the first element of this array of names (or `undefined` if there is no such place found) – derpirscher Jan 17 '21 at 22:20
  • man, you saved me. thanks a lot. I didn't realize that whereAreWe was an array. The whereAreWe.map(place => place.name)[0] worked. – A. Curvina Jan 17 '21 at 22:36
  • 1
    For your usecase you should probaly use [`find`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Array/find) which does not return an array but the first element found within the array (or undefined if no element is found) – derpirscher Jan 18 '21 at 14:33

0 Answers0