我收到一个包含大量信息的JSON,但我想删除一个特定的数据,例如,如果我收到变量名,我想在我的数据库中添加我的JSON之前删除这个变量.这是我的功能POST:
app.post("/:coleccion", function (req, res, next) {
POST
if(req.body.name)
// Here I need delete "name" if I receive this variable <------
req.collection.insert(req.body, {}, function (e, result) {
if(e) return next(e);
res.send(result);
});
});
Run Code Online (Sandbox Code Playgroud) 我有这个数组:
var myArray = [
{first_name: "Oded", last_name: "Taizi", id: 1},
{first_name: "Ploni", last_name: "Almoni", id: 2}
];
Run Code Online (Sandbox Code Playgroud)
我想删除 id 元素?我创建了这样的函数,但它不能正常工作。
function removeKeys(array,keys){
for(var i=0; i<array.length; i++){
for(var key in array[i]){
for(var j=0; j<keys.length; j++){
if(keys[j] == key){
array[i].splice(j, 1);
}
}
}
}
}
removeKeys(myArray ,["id"]);
Run Code Online (Sandbox Code Playgroud)
结果数组应如下所示:
[
{first_name: "Oded", last_name: "Taizi"},
{first_name: "Ploni", last_name: "Almoni"}
];
Run Code Online (Sandbox Code Playgroud) 只是觉得对一些测验感到困惑.
测验一
var x = 1;
if (function f(){}) {
x += typeof f;
}
alert(x);Run Code Online (Sandbox Code Playgroud)
回答:
x的答案是"1 undefined"
混乱:
在条件有效的情况下,"函数f(){}"到底发生了什么?
测验二
(function(x){
delete x;
return x;
})(1);Run Code Online (Sandbox Code Playgroud)
回答:
输出为1.
混乱:
为什么删除不起作用?什么时候'删除'工作,什么时候不工作?
如何使用元素的位置删除元素?例如,我想删除第二个。
Object {duur: ".short", taal: ".nl", topic: ".algemeen-management"}
Run Code Online (Sandbox Code Playgroud) 我想从数组中删除位置.当我尝试delete $products.position;它不是删除.但是当我尝试delete $products[0].position;它只是从第一次删除.
我试图搜索解决方案很多,但没有人真正帮助我.
我有一个javascript对象:
trucks_obj
= Object {95: _.Kd, 96: _.Kd}
95: _.Kd
96: _.Kd
__proto__: Object
Run Code Online (Sandbox Code Playgroud)
如果if条件为真,我只想从具有特定ID的trucks_obj中删除元素.例如,我需要删除95索引的元素.我已经尝试过splice()以及slice()但没有任何效果.
删除代码:
$.each(trucks_obj,function(i,e){
if(($.inArray(i, trucks_arr)) === -1){
trucks_obj.splice(i,1);
}
});
Run Code Online (Sandbox Code Playgroud) 说我有一个像这样的对象:
obj = {
property1: "Prop 1 value",
property2: "-",
property3: "Prop 3 value"
property4: "-"
}
Run Code Online (Sandbox Code Playgroud)
如果属性值等于" - "属性我想从对象中删除它.
我已经研究了循环,但我甚至不知道这是否是使用对象的正确范例.