1

I'm trying to create a suite of histograms for a website. I want to created a mapped variable that only pulls out the speed data if the character has a specific rank. My data set looks like the following (I've bolded the relevant variables that I want to deal with):

myData = [{name:'Maru', rank:'L', speed:117.6},{name:'Kharg', rank:'G', speed:114.4}...and so on...]

I'm trying to use an if statement (pseudocode: if myData.rank = 'L'...) in conjunction with the following:

var speedData = myData.map(function(i){ return i.speed; })

However, I'm not having any luck. Here's a sample of what I'm trying (among other things, but I suspect that this is the closest to working):

    var lightData = myData.map(function(i){
        if (i.rank == 'L'){
            return i.speed;}
            })

Is this possible using just js and D3? I suspect that something like Django might make this easier, but it's an assignment that limits to just js and D3.

I realize that if my data set was smaller, I could manually separate all the ranks and then do a merge when I need to run all the data at once. However, that's just not practical with the data I have.

1 Answer 1

1

With plain Javascript, you could use a chained approach by filtering first and then map the value, you need.

var result = myData
        .filter(function (o) { return o.rank === 'L'; })
        .map(function (o) { return o.speed; });
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.