0

I have an array of objects each of that has a lot of keys (> 200). Some values of these key can be "-", "" or "na". I want to replace these values with null.

Example:

[
  {
    "waste ": "",
    "d_f": "-",
    "sa": "  - ",
    "fgdf-h": "na",
    "gdws": "0",
    "ffd ": "121.4",
    " tg": "34",
    "yyy rd": "1,23 ",
    "abd": " 45678",
    "abd": " 4.567,8",
    "edf": " na",
    "first": "-   ",
    ...
  },
  {...},
]

must become:

[
  {
    "waste ": null,
    "d_f": null,
    "sa": null,
    "fgdf-h": null,
    "gdws": "0",
    "ffd ": "121.4",
    " tg": "34",
    "yyy rd": "1,23 ",
    "abd": " 45678",
    "abd": " 4.567,8",
    "edf": null,
    "first": null,
    ...
  },
  {...},
]

This is my code:

function uniformNoData(data) {
  const result = data.map((datum, i) => {
    return Object.values(datum).map(d => {
      // and then ??
    })
    return datum
  })
  return result
}

I don't know how to complete the code.. I want a new object and not modify data object so I used map and not foreach.

Any help is appreciated

6 Answers6

0

Use forEach loop on the Object.keys and replace the values

var a = [{
  "waste ": "",
  "d_f": "-",
  "sa": "  - ",
  "fgdf-h": "na",
  "gdws": "0",
  "ffd ": "121.4",
  " tg": "34",
  "yyy rd": "1,23 ",
  "abd": " 45678",
  "abd": " 4.567,8",
  "edf": " na",
  "first": "-   ",
}]
var obj = {};
a.forEach((x) => {
  Object.keys(x).forEach((e) => {
    if (x[e].trim() == "na" || x[e].trim() == "-" || x[e].trim() == "")
      obj[e] = null;
    else
      obj[e] = e;
  })
})

console.log(obj)
ellipsis
  • 12,049
  • 2
  • 17
  • 33
0

Working example: https://jsfiddle.net/0gdr9t7f/

const badVals = ['-', 'na'];   

const newArr = arr.map(entry => {
    Object
    .keys(entry)
    .forEach(key => {
         const val = entry[key].trim();
         if (!val || badVals.includes(val)) {
            entry[key] = null;
         }
    })
    return entry;
});
lux
  • 8,315
  • 7
  • 36
  • 49
0

You could collect the replacement strings in an array and check against.

var data = [{ "waste ": "", d_f: "-", sa: "  - ", "fgdf-h": "na", gdws: "0", "ffd ": "121.4", " tg": "34", "yyy rd": "1,23 ", abd: " 4.567,8", edf: " na", first: "-   " }],
    nullValues = ['', '-', 'na'],
    result = data.map(o => Object.assign({}, o, ...Object.keys(o).filter(k => nullValues.includes(o[k].trim())).map(k => ({ [k]: null }))));

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

You can't use Object.values() for this. You have to iterate over Object.keys() so that you have the key to update the entry with.

function uniformNoData(data) {
  data.map((datum) => {
    datum = clone(datum);
    Object.keys(datum).forEach(key => {
      if (datum[key] == "na" || datum[key] == "-" || datum[key] == "") {
        datum[key] = null;
      }
    });
    return datum;
  })
}

See How do I correctly clone a JavaScript object? for various ways to implement the clone() function I called above.

Barmar
  • 741,623
  • 53
  • 500
  • 612
0

I'd create an array with values that should be replaced, than map as you did

const data = [
    {
        "waste ": "",
        "d_f": "-",
        "sa": "  - ",
        "fgdf-h": "na",
        "gdws": "0",
        "ffd ": "121.4",
        " tg": "34",
        "yyy rd": "1,23 ",
        "abd": " 45678",
        "abd": " 4.567,8",
        "edf": " na",
        "first": "-   ",
    },
];

const garbage = ["", "na", "-"];

const cleanData = data.map(e => {
    const obj = {};
    for  (let key in e) {
        obj[key] = garbage.includes(e[key].trim()) ? null : e[key];
    }
    return obj;
});


console.log(cleanData);
baao
  • 71,625
  • 17
  • 143
  • 203
0

You can use forEach and regex pattern.

^(|[- ]+|\s*na\s*)$
  • ^ - Starting of string.

  • (|[- ]+|\s*na\s*) - Matches empty string, string with - and space, matches any na with any number of spaces at front and back.

  • $ - End of string.

let obj = { "waste ": "","d_f": "-","sa": "  - ",  "fgdf-h": "na","gdws": "0","ffd ": "121.4"," tg": "34","yyy rd": "1,23 ","abd": " 45678","abd": " 4.567,8","edf": " na","first": "-   ",}
  
let newObj = {}
  
let op = Object.keys(obj).forEach(e=>{
  newObj[e] = /^(|[- ]+|\s*na\s*)$/g.test(obj[e]) ? null : obj[e]
})

console.log(newObj)
Code Maniac
  • 37,143
  • 5
  • 39
  • 60