0

The following functions loops through a list. If a particular item within a sublist has a blank value, then it replaces it with "None."

lst.forEach(function (d) {

if(d.thefirstcolumn){} else {d.thefirstcolumn = "None"};
if(d.somethingelse){} else {d.somethingelse = "None"};
if(d.random){} else {d.random = "None"};
if(d.cold){} else {d.cold = "None"};
if(d.cole){} else {d.cole = "None"};
if(d.colf){} else {d.colf = "None"};
if(d.colg){} else {d.colg = "None"};
if(d.colh){} else {d.colh = "None"};
});

This is not very scalable, because I have to reference every item within every sublist, so I'd to create a function that does this for all items so that I don't have to reference.

For example, it would be something short like the following where it would accomplish the same result as the previous code and do it for every item within every sublist of the list.

lst.forEach(function (d) {
if(d.col){} else {d.col = "None"};
});

Above, I mean d.col to refer to d.cola, d.colb, d.colc, d.cold, and all other items. How would I make this work?

Bonus question is that how would I apply the following function as well only if the the item within the sublist is a number otherwise just apply the previous function?

For example,it would try this on each item of the sublist

d.col = parseInt(d.col, 10);

Otherwise it would run the following if the item was not a number

if(d.colh){} else {d.colh = "None"}
Chris
  • 5,444
  • 16
  • 63
  • 119
  • Use `d[col]` when the column name is in `col`. – Barmar Aug 16 '14 at 12:29
  • okay, so i need to change my example, because the column name can vary a lot. – Chris Aug 16 '14 at 12:31
  • 1
    So? However it can vary, you should be able to get all of them into the `col` variable. You can make an array of all the possible values, and iterate over that. – Barmar Aug 16 '14 at 12:34
  • 1
    `var cols = ["cola", "colb", ...];` Then inside your `forEach` function, you do `cols.foreach(function(col) ...` and then use `d[col]`. – Barmar Aug 16 '14 at 12:37
  • 1
    For the parseInt, it's `d[col] = parseInt(d[col], 10)`. – Barmar Aug 16 '14 at 12:38

1 Answers1

2

Do some thing like bellow

var keys = ['firstCol','secondCol','cola'];

lst.forEach(function (d) {
    keys.forEach(function(key){
         d[key]=d[key]||"None";
    })
});

So it will get each object & check for the object if key is not present assign none to that key.

Mritunjay
  • 25,338
  • 7
  • 55
  • 68
  • If the key isn't present, it won't show up in the `for (var key in d)` loop. This will only work if the key is present but has an empty value. – Barmar Aug 16 '14 at 12:34
  • @Barmar updated my answer, in situation. – Mritunjay Aug 16 '14 at 12:42
  • @Chris I didn't get what you mean. – Mritunjay Aug 16 '14 at 12:47
  • Well, your function works great and does the job - but i was wondering if there was a way we could apply a function to all keys and run a different function if that function returns an error. For example, I'd like to apply this: keys.forEach(function(key){d[key] = parseInt(d[key], 10)}); but have it only work for the values that were string numbers and apply d[key]=d[key]||"None"; if there was an error. – Chris Aug 16 '14 at 12:57