0

I have been trying to figure out how to simply search and replace a value (in this case replacing any value equal to null with undefined) in an array of objects like this - the array keys could vary:

var array = [{ 
  "name": "mike",
  "age": null
},
{ "name": "jim",
  "age": 99
}];

Expected result:

var array = [{ 
    "name": mike,
    "age": undefined
  },
  { "name": jim,
    "age": 99
  }];

My impression is that I should be able to do this using the map() function but none of the documentation examples are quite making sense to me for this. I have been trying to apply the solution from this question: https://stackoverflow.com/a/5915891/2930969 but without any success.

Anyways, if anyone cares to help point me in the right direction here is a framed out codepen you can tinker with: http://codepen.io/anon/pen/zBxwdj?editors=0012

6
  • 1
    what is your expected result..? Commented Jun 1, 2016 at 5:13
  • Would help to have valid json, mike and jim are undefined. Commented Jun 1, 2016 at 5:15
  • @PranavCBalan updated Commented Jun 1, 2016 at 5:17
  • @Ultimater fair point, fixed Commented Jun 1, 2016 at 5:17
  • falling asleep, will revisit this in the morning. thanks. Commented Jun 1, 2016 at 5:36

7 Answers 7

2

Since you said that the keys may vary, you will need to do two loops to cover arbitrary key names. One over the array, and then one over the keys of each object.

var nullToUndef = function (a) {
    return a.map(function (o) {
     Object.keys(o).forEach(function (key) {
       if (o[key] === null) {
         o[key] = undefined;
       }
      });
      return o;
    });
  },
  array = [
    { 
      "name": "mike",
      "age": null
    },
    { "name": "jim",
      "age": 99
    },
    {"foo": null,
    "bar": null
    }
  ];

console.log(nullToUndef(array));

jsFiddle

nullToUndef uses Array.prototype.map to create a new array, inside of the mapping function it uses Object.keys to get a list of the key names on each object. It then checks each property value to see if it is null and changes null properties to undefined before returning the object for the new array.

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

Comments

1

var array = [{ 
  "name": "mike",
  "age": null
},
{ "name": "jim",
  "age": 99
}];

array = array.map(function(item) {
  if(item.age === null) {
    // delete item.age;
    item.age = undefined;
  }
  
  return item;
});

console.log(array);

1 Comment

This is very close to what I'm thinking. I was able to get the result I was looking for by switching 'item.name === mike' to 'item.age === null' - if you switch that I'll mark it as correct
1

Use forEch() method to iterate and update

var array = [{
  "name": "mike",
  "age": null
}, {
  "name": "jim",
  "age": 99
}];

array.forEach(function(v) {
  v.age === null && (v.age = undefined)
  //or
  // if(v.age === null) v.age = undefined
})

console.log(array);

Comments

0

Filter down your array using some unique/distinguishing property (name in this case? ids are helpful here) and then update the object directly:

var mike = array.filter(function(obj) { return obj.name === 'mike' })[0];
mike.age = undefined;

Comments

0

try it,

updated to only change value where name is 'mike' and age is NULL.

note: it's good check existence of property in object before using it.

var array = [{ 
      "name": "mike",
      "age": null
    },
    { "name": "jim",
      "age": 99
    }];

var arr = [];

arr = array.map(function(x) { 
              if(x.hasOwnProperty("age") && x.hasOwnProperty("name") && x["age"] == null && x["name"] == "mike")
                            x["age"] = undefined;
  return x;
                          });

console.log(arr);

3 Comments

Seems to be replacing all "age" with the value undefined, even if it didn't start as null
from OP question: in this case replacing null with undefined
Sorry, may have been unclear, didn't want to make all 'age' values undefined, just the ones that had originally been null
0

And the reduce way.

var arr = [{ 
  "name": "mike",
  "age": null
},
{ "name": "jim",
  "age": 99
}],
  fixed = arr.reduce((p,c) => (c.age === null && (c.age = void 0), p.concat(c)),[]);
console.log(fixed);

Comments

0

the shortest possible code is as below

array.map(a => (a.age = a.age === null ? undefined : a.age))

console.log(array)

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.