简体   繁体   中英

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

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.

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

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

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

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;

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

the shortest possible code is as below

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

console.log(array)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM