如何如下重置对象。 作为静态对象,我可以使用下面的对象,但是让该对象可以包含其他字段或更少的字段,然后将其动态提供。 如何清空此对象,如下所示。
var object={
id:0,
clientId:0,
position:'',
positionCnt:'',
InsValue:'',
Code:'',
FieldName:''
};
答案 0 :(得分:2)
您可以设置一个对象,该对象可以按类型(console.log("starting...");
const incidences = new L.GeoJSON.AJAX("http://127.0.0.1:8000/incidence_data/", {
onEachFeature: function (feature, layer) {
//console.log(feature.properties);
layer.bindPopup(feature.properties.name.toString())
}
});
incidences.addTo(map);
console.log("... not loaded yet!");
$("#buffer").click(function () {
const incidences2 = incidences.toGeoJSON();
console.log(incidences2);
if ($("#buffer").html() == 'Buffer') {
var buff = turf.buffer(incidences2, 1, {'units': 'miles'});
console.log(buff);
bufferLayer = L.geoJSON(buff).addTo(map);
$("#buffer").html("Remove Buffer");
} else {
map.removeLayer(bufferLayer);
$("#buffer").html("Buffer");
}
});
,string
等)定义默认值。
使用此定义后,以下内容将循环遍历该对象,并将属性值设置为其默认类型。
重要:下面的代码是一个可靠的开始,但是它不能处理number
,嵌套对象等,因此您必须对其进行充实。>
arrays
我添加了第二个(可选)参数var defaultTypeValues = {
number: 0,
string: '',
boolean: false
};
var obj = {
id: 525,
clientId: 1624,
position: 'pos',
positionCnt: 'abc',
InsValue: 'def',
Code: 'abc123',
FieldName: 'fieldname'
}
function emptyObj(obj, defaultOverrides = {}) {
var defaults = Object.assign({}, defaultTypeValues, defaultOverrides);
Object.keys(obj).forEach(k => obj[k] = defaults[typeof obj[k]]);
return obj;
}
console.log(emptyObj(obj));
,您可以在其中指定覆盖默认值的值,即defaultOverrides
将所有布尔值替换为emptyObj(obj, {boolean: true});
而不是默认true
。
答案 1 :(得分:1)
var a = {one: 1, two: '2', three: 3, four: true}
let sort = (obj, val, val2, val3) => {
Object.keys(obj).forEach(x => {
if (typeof obj[x] === 'string') {
obj[x] = val;
} else if (typeof obj[x] === 'number') {
obj[x] = val2;
} else if (typeof obj[x] === 'boolean') {
obj[x] = val3;
} else {
// Extend here to datatypes
}
});
}
let set = obj => sort(obj, '', 0, false); // Additional types
set(a)
console.log('After:', a)
答案 2 :(得分:0)
保留模板对象,以使您知道要将其重置为什么。
然后遍历并擦除所有属性,并在其上分配模板。
res_mat <- matrix(0, nrow=2, ncol=5)
for(i in seq_along(vec_of_df_names)) {
res <- which(colSums(get(vec_of_df_names[i])) < 0.1)
if(length(res)>0) res_mat[i, seq_along(res)] <- res
}
res_mat
答案 3 :(得分:0)
您可以使用Object.assign
let newObject = Object.assign(object, {position: "x"});
var object={
id:0,
clientId:0,
position:'',
positionCnt:'',
InsValue:'',
Code:'',
FieldName:''
};
let newObject = Object.assign(object, {position: "x"});
console.log(newObject);
或传播算子...obj
let newObject = { ...object, position: "x"};
var object = {
id: 0,
clientId: 0,
position: '',
positionCnt: '',
InsValue: '',
Code: '',
FieldName: ''
};
let newObject = { ...object,
position: "x"
};
console.log(newObject);
答案 4 :(得分:0)
这里是使用嵌套对象处理原型的示例。
它不会使用超出范围,易于测试和维护的变量。
const toDefault = defaults => {
return function resetter(o) {
Object.keys(o).forEach(key => {
if (typeof o[key] === "object") {
o[key] = resetter(o[key]);
} else {
o[key] = defaults[typeof o[key]];
}
})
return o;
}
}
const reset = toDefault({string: "", number: -1});
var obj = {
id: 1230,
clientId:0,
position:'',
positionCnt:'',
InsValue:'',
Code:'12312312',
FieldName:'',
nested: {
a:12312,
b:123123
}
};
console.log(reset(obj));