如何在对象文字数组中搜索和替换值

时间:2016-06-01 05:10:57

标签: javascript arrays

我一直在试图弄清楚如何在这样的对象数组中简单地搜索和替换一个值(在这种情况下用undefined替换任何等于null的值) - 数组键可能会有所不同:

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

预期结果:

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

我的印象是我应该可以使用map()函数来完成此操作,但是没有一个文档示例对我来说非常有意义。我一直在尝试应用这个问题的解决方案:https://stackoverflow.com/a/5915891/2930969但没有任何成功。

无论如何,如果有人愿意帮助我指出正确的方向,那么你可以修改一个框架式的编码:http://codepen.io/anon/pen/zBxwdj?editors=0012

7 个答案:

答案 0 :(得分:1)

使用 forEch() 方法进行迭代和更新

@Aspect
public class HijackBeforeMethod {

@Pointcut("within(@org.springframework.stereotype.Controller *)")
public void controllerBean() {
}

@Pointcut("execution(* *(..))")
public void methodPointcut() {
}

@Before(value = "controllerBean() && methodPointcut()", argNames = "joinPoint")
public void before(JoinPoint joinPoint) {

  MethodSignature signature = (MethodSignature) joinPoint.getSignature();
  Object[] args = joinPoint.getArgs();
  String[] paramNames = signature.getParameterNames();
  for (int count = 0; count < paramNames.length; count++) {
     String tempParam = paramNames[count];
     Object tempValue = args[count];
     if (tempParam.toLowerCase().equalsIgnoreCase("codeAccount") && Assert.isNotNull(tempValue)
           && Assert.isNotEmpty((String) tempValue)) {
        Assert.TRUE(((String) tempValue).matches("^[0-9]{1,20}$"));
     }
  }
}
}

答案 1 :(得分:1)

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 2 :(得分:1)

由于您说密钥可能会有所不同,因此您需要执行两个循环来覆盖任意密钥名称。一个在数组上,然后一个在每个对象的键上。

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使用Array.prototype.map创建一个新数组,在它使用Object.keys的映射函数内部获取每个对象上的键名列表。然后,在返回新数组的对象之前,它会检查每个属性值以查看它是否为null并将null属性更改为undefined

答案 3 :(得分:0)

试试吧,

更新为仅更改名称为'mike'且age为NULL的值。

注意:在使用之前很好地检查对象中是否存在属性。

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

答案 4 :(得分:0)

使用一些唯一/区别属性(在这种情况下名称?这里有用)来过滤你的数组,然后直接更新对象:

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

答案 5 :(得分:0)

减少方式。

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 6 :(得分:0)

最短的代码如下

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

console.log(array)