0

So I have loaded a JSON object from D3 and now want to filter out some columns from the array. My only problem is that doing:

Object.attribute; 

doesn't return what I want. The structure of my JSON object is as follows. It holds 1000 objects and each array hold attributes inside of it. I am getting my data from https://data.raleighnc.gov/resource/xce4-kemu.json, for reference.

If I do

Object[0].attribute;

It returns the actual value of the object, so like "12". Where as I want it to return all the values where that key is present

In the URL you can see an example of the JSON. I want to, for example return a JSON where only the issueddate_mth, statelicnum, and constcompletedofficial attributes are present.

An Snippet of what I tried is:

 var raleigh_data = "https://data.raleighnc.gov/resource/xce4-kemu.json";
d3.json(raleigh_data, function(error, data) {
var filtered_data = data.filter(function(d,i,e) {
return d.issueddate_mth;
});

}

5
  • it's an array of 1000 objects, not 1000 arrays Commented Oct 11, 2016 at 2:22
  • 1
    what do you want to filter from that data? Commented Oct 11, 2016 at 2:24
  • 1
    use something like var filteredArray = xxx.filter(callbackFunction) to filter an array (the array in this case is a var called xxx ... you dont want to use a var called Object) ... callbackFunction is a function that accepts 3 arguments, the item, the index and the whole array ... do your logic and return true or false to include/exclude the item in the new array returned Commented Oct 11, 2016 at 2:24
  • So am I understanding correctly-- you want to iterate over every object in the array and return an array of all values for a given property present in each object? Commented Oct 11, 2016 at 2:25
  • @anied I updated my answer to hopefully make it more clear Commented Oct 11, 2016 at 2:41

1 Answer 1

3
var data = [your array of 1000 objects]
var filteredData = [];
data.forEach(function(item) { 
    if (item.issueddate_mth && item.statelicnum && item.constcompletedofficial) {
        filteredData.push(item);
    }
});

This will get you an array that only contains items from your original array with the specified properties.

Also, I am not 100% sure if you only want those three properities. If so, you could easily just push { issueddate_mth: item.issueddate_mth, statelicnum: item.statelicnum, constcompletedofficial: item.constcompletedofficial } instead of item in the forEach callback

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

2 Comments

Is there a way to do the reverse. Meaning that I take out properties that I don't want in the the JSON Object?
Ok I tried the second part as you specified and it worked like a charm. Thank you!!

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.