2

I have this array of objects shown below

Object {Results:Array[3]}
    Results:Array[3]
        [0-2]
             0:Object
                    id=null     
                    name: "Rick"
                    Value: "34343"
             1:Object
                    id=2     
                    name: null
                    Value: "2332"
             2:Object
                    id=2     
                    name:'mike'
                    Value: null

As you can see, in 1 object i have id as null, 2nd object has name as null and 3rd object has Value as null. Each object has some property as null.

I want to loop through all of these and replace null with ''. Can someone let me know how to achieve this...

4
  • Do you know property names upfront? Commented Sep 7, 2016 at 16:05
  • yeah. these 3 properties. id, name and Value. Commented Sep 7, 2016 at 16:06
  • Then, what is the problem? forEach and 3 if inside. Commented Sep 7, 2016 at 16:08
  • You can do it by looping through all of the elements and replacing null with ''. Commented Sep 7, 2016 at 16:11

4 Answers 4

5

Here's something quick:

var results = [{
    id: null,
    name: "Rick",
    Value: "34343"
}, {
    id: 2,
    name: null,
    Value: "2332"
}, {
    id: 2,
    name: 'mike',
    Value: null
}];

results.forEach(function(object){
    for(key in object) {
        if(object[key] == null)
            object[key] = '';
    }
});

console.log(JSON.stringify(results, null, 2))
Sign up to request clarification or add additional context in comments.

Comments

3

You only needed to Google looping through objects. Here's an example:

Looping through every key (if your keys are unknown or you want to do the same for all keys)

for (const obj of arr) {
  if (typeof obj !=== 'object') continue;
  for (k in obj) {
    if (!obj.hasOwnProperty(k)) continue;
    v = obj[k];
    if (v === null || v === undefined) {
      obj[k] = '';
    }
  }
}

If your keys are known:

for (const obj of arr) {
  if (obj.name === undefined || obj.name === null) {
    obj.name = '';
  }
}

Comments

2

You could iterate over the elements of the array and over the keys and assign the wanted strings if necessary.

var data = { Results: [{ id: null, name: "Rick", Value: "34343" }, { id: 2, name: null, Value: "2332" }, { id: 2, name: 'mike', Value: null }] };

data.Results.forEach(function (o) {
    Object.keys(o).forEach(function (k) {
        if (o[k] === null) {
            o[k] = '';
        }
    });
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Comments

2

You can look over your array and all keys and if the property contains null replace it with empty string, e.g.:

var arr = [
  { id=null name: "Rick" Value: "34343" },
  { id=2 name: null Value: "2332" },
  { id=2 name:'mike' Value: null }
];

arr.forEach(function(o) {
    Object.keys(o).forEach(function(key) {
      id (o[key] === null) {
        o[key] = '';
      }
    });
});

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.