How to calculate total number of unique locations in an array of objects?

Viewed 63

https://jsfiddle.net/n5jy4ozf/1/

window.onload = function (){
    let filHenter = document.getElementById("file");
    filHenter.addEventListener("change", grabFileObject)
}


//Task 2 and Task 3
function grabFileObject(event){
    //Grabs the uploaded file
    let file = event.target.files[0];
    let filLeser = new FileReader();

    //Reads the file as text and calls anonymous function on load
    filLeser.readAsText(file);
    filLeser.onload = function (){
        //Splits each line
        const lines = filLeser.result.split("\n");

        //Loop to iterate through each line
        for (var i = 0; i < lines.length; i++){
            //Splits each line in an element
            lines[i] = lines[i].split(",");
            //Saves elements to an object.
            lines[i] = {Date : lines[i][0],
                        Type : lines[i][1],
                        Location : lines[i][2],
                        Amount : parseFloat(lines[i][3])}
        }
        console.log(lines);
        mainfunction(lines);
    }
}

function mainfunction(array){
    console.log("Total number of locations: ", totalNumberOfLocations(array));
}

// Task 4.1
function totalNumberOfLocations() {
}

I have an assignment where I have to read a CSV file in JavaScript and display the data and its statistics. I've managed to finish the part where I have to read the files and save each transaction in an array of objects. My question now is how do I calculate statistics for these transactions.

If I want to calculate the total unique number of locations (non-duplicate) in the list, how would I go about doing this using my the setup I already have?

3 Answers

you have to save somewhere all the results to see if its unique or not. For example like this:

const data = [{Location: 'x'}, {Location: 'x'}, {Location: 'y'}];

function totalNumberOfLocations(array) {
  const unique = {};
  array.forEach(item => (unique[item.Location] = item));
  // Now in unique object you have all unique fields by Location. You can i.e. return and parse it.
  // To just count it, you can use following
  console.log('length of unique locations is', Object.keys(unique).length);
}

totalNumberOfLocations(data);

You can use Set for unique values, after mapping only the location of the objects. Then you can get the count of unique locations by converting this set back to an array and using length property

const data = [{Location: 'x'}, {Location: 'x'}, {Location: 'y'}];
const getUniqueLocations = data => [...new Set(data.map(i => i.Location))].length;
console.log(getUniqueLocations(data)); // prints 2

For finding anything unique in an array, I use filter and compare the index to findIndex (unique by property) or indexOf (unique by primitive or object ref) because they return the index of only the first element that matches.

const data = [{Location: 'x'}, {Location: 'x'}, {Location: 'y'}];
const unique = data.filter((obj, index, array) => index === array.findIndex(o => o.Location === obj.Location));
console.log(unique.length); // -> 2

Related