Neal Jones
Neal Jones

Reputation: 459

How to search and replace value in array of object literals

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

Upvotes: 0

Views: 1878

Answers (7)

Kapilrc
Kapilrc

Reputation: 1538

the shortest possible code is as below

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

console.log(array)

Upvotes: 0

Useless Code
Useless Code

Reputation: 12412

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.

Upvotes: 2

Yordan Ivanov
Yordan Ivanov

Reputation: 580

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);

Upvotes: 1

Redu
Redu

Reputation: 26171

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);

Upvotes: 0

A.T.
A.T.

Reputation: 26312

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);

Upvotes: 0

tomvalorsa
tomvalorsa

Reputation: 292

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;

Upvotes: 0

Pranav C Balan
Pranav C Balan

Reputation: 115232

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);

Upvotes: 1

Related Questions