0

I have data in a CSV file that looks like this:

Year      Teams     Matches    Goals
2014      32        64         171
2010      32        64         125
2006      32        64         147
2002      32        64         161
1998      32        64         171
1994      24        52         141

enter image description here

I ingest the data using the following code:

let promises = [
    d3.csv('data/test-data.csv', d3.autoType),
];

Promise.all(promises)
    .then(function (data) {
        createVis(data)
    })
    .catch(function (err) {
        console.log(err)
    });

Then, I have a function createVis() that wrangles the data and creates a graph:

function createVis(data) {

    let graphData = data[0]

    wrangleData()

}

In my use case, the wrangleData() function would only grab the Year and Matches columns of the original data set.

Something like this:

function wrangleData(data) {

let filteredData = []

filteredData = data.filter...

}

filterdData would look like this:

Year      Matches   
2014      64        
2010      64        
2006      64        
2002      64        
1998      64        
1994      52        

How would I do this?

Thanks!

3
  • 4
    Why would you want to do that? That seems like a rather expensive operations with little to no benefits. You can safely ignore any properties that are not needed in your code processing the ingested data. This has some smell of an XY-problem. Commented Nov 28, 2022 at 19:24
  • @altocumulus -- Are you suggesting that I get rid of any unnecessary data when ingesting the file? If yes, that's not possible. I need all data - one piece for this instance of wrangleData() and another piece for a separate instance of wrangleData() in another class. Commented Dec 5, 2022 at 2:33
  • You are not filtering your data, you are just manipulating it for no reason. The creation of new objects having only the needed properties, however, unnecessarily takes computing time and memory. Performance-wise you should just refer to your original data and access only those properties you are interested in during any run of wrangleData(). Commented Dec 5, 2022 at 10:48

1 Answer 1

2

To reduce the amount of keys in the objects, use map() with object destructuring to return an object with the desired keys:

const data = [
  { Year: '2001', Teams: '32', Matches: '64', Goals: '161' },
  { Year: '2011', Teams: '53', Matches: '33', Goals: '111' }
];

const res = data.map(({ Year, Matches }) => ({ Year, Matches }));
console.log(res)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.