1

How to select the numerical value elements and how to ignore the alphabatical elements(in this example "2009-10": "NA", "2010-11": "NA" ).

stateData:[ {"1999-00": "125236",
              "2000-01": "140119",
              "2001-02": "151482",
              "2002-03": "163385",
              "2003-04": "184463",
              "2004-05": "202575",
              "2005-06": "239683",
              "2006-07": "277286",
              "2007-08": "326547",
              "2008-09": "377346",
              "2009-10": "NA",
              "2010-11": "NA" }]

6 Answers6

1

Try map and inside that take entries of object and then filter the ones which do not match digit regex /\d+/. Something like this:

var stateData=[ {"1999-00": "125236", "2000-01": "140119", "2001-02": "151482", "2002-03": "163385", "2003-04": "184463", "2004-05": "202575", "2005-06": "239683", "2006-07": "277286", "2007-08": "326547", "2008-09": "377346", "2009-10": "NA", "2010-11": "NA" }];

var result = stateData.map(p=>Object.fromEntries(Object.entries(p).filter(([k,v])=>v.match(/\d+/))));

console.log(result);
gorak
  • 4,839
  • 1
  • 4
  • 16
1

You can use a simple regular expression /^\d+$/ to check the string value has only numbers or not. If the value passes the regular expression, you can select it and ignore it if fails

let result = stateData.map(value => {
    const temp = {};
    for(var key in value) {
        if(/^\d+$/.test(value[key])
         temp[key] = value[key]
    }
    return temp;
}

//result array will have all the values which are numbers
console.log(result);
Anand Raj
  • 350
  • 1
  • 7
1

You could do something like this (use /^\d+$/ to test if it's all numerical):

const stateData = [{
  "1999-00": "125236",
  "2000-01": "140119",
  "2001-02": "151482",
  "2002-03": "163385",
  "2003-04": "184463",
  "2004-05": "202575",
  "2005-06": "239683",
  "2006-07": "277286",
  "2007-08": "326547",
  "2008-09": "377346",
  "2009-10": "NA",
  "2010-11": "NA3"
}]


console.log(stateData.map(
  x =>
  Object.fromEntries(Object.entries(x).filter(([_, val]) => /^\d+$/.test(val)))
))
Tibebes. M
  • 4,961
  • 4
  • 9
  • 30
1

You can is isNaN() to check whether the value is number or not.

var stateData = [{
  "1999-00": "125236",
  "2000-01": "140119",
  "2001-02": "151482",
  "2002-03": "163385",
  "2003-04": "184463",
  "2004-05": "202575",
  "2005-06": "239683",
  "2006-07": "277286",
  "2007-08": "326547",
  "2008-09": "377346",
  "2009-10": "NA",
  "2010-11": "NA"
}]
var result = stateData.map(p=>Object.fromEntries(Object.entries(p).filter(([k,v])=> !(isNaN(v)))));

console.log(result)
Rajan
  • 1,314
  • 2
  • 13
  • 17
1

This is as short as it gets, do try

let stateData=[{"1999-00": "125236",
                "2000-01": "140119",
                "2001-02": "151482",
                "2002-03": "163385",
              "2003-04": "184463",
              "2004-05": "202575",
              "2005-06": "239683",
              "2006-07": "277286",
              "2007-08": "326547",
              "2008-09": "377346",
              "2009-10": "NA",
              "2010-11": "NA"}]

let result = []

for(let key in stateData[0]) {
  if(+stateData[0][key]) {
    result.push(stateData[0][key])
  }
}


console.log(result)
DpK
  • 100
  • 4
1

The way I would go about this kind of a problem in general, is:

  1. I would first convert the object into an array of the relevant data. In this case Object.entries will serve our purpose well, it gives us an array of "tuples" (a tuple is just an array), where each tuple consists of 2 values, a KEY and its corresponding VALUE.
  2. We can then filter the array of tuples, this is where we find out if the give value is a valid number. We do this by attempting to parse it as a number, and the interpreting that result as a boolean. The key thing to know here is that Number("NA") returns NaN (not a number), and Boolean(NaN) is false (worth noting, Boolean(0) is also false, so this method only works if you know for a fact that the numbers wont ever be equal to 0).
  3. Finally we can turn this filtered list of tuples back into an object, using Object.fromEntries.

const input = {
  "1999-00": "125236",
  "2000-01": "140119",
  "2001-02": "151482",
  "2002-03": "163385",
  "2003-04": "184463",
  "2004-05": "202575",
  "2005-06": "239683",
  "2006-07": "277286",
  "2007-08": "326547",
  "2008-09": "377346",
  "2009-10": "NA",
  "2010-11": "NA"
};

const output = Object.fromEntries(
  Object.entries(input)
    .filter(([k, v]) => 
      Boolean(Number(v))
    )
);

console.log(output);
Olian04
  • 4,754
  • 2
  • 23
  • 46