我需要从存储在数组中的对象中删除空属性,而不需要触摸原始数组。嵌套for循环,有没有更好的方法?
这里是我的代码:
// first I create a copy of the array
parsedShapes = [];
for (var i in shapes) {
parsedShapes.push(shapes[i]);
}
// then for each objects in the new array, I delete all the empty attributes.
for (var i in parsedShapes) {
var objects = parsedShapes[i];
for (var j in objects) {
if (objects[j] === "") {
delete objects[j];
}
}
}
此代码的工作,但我想知道是否有更好的方法来处理这个操作。
谢谢。
使用'.map()'和'.forEach()'。 – SLaks