[英]Cut data from array of JSON objects
I have array of JSON objects like this: 我有这样的JSON对象数组:
[ { id: 841,
when: 'date',
who: 'asd',
what: 'what',
key1 : 'a key',
key2 : 'a key'
_id: 544034ab914ae3b9270545c1,
__v: 0 },
{ id: 841,
when: 'date',
who: 'asd',
what: 'what',
key1 : 'a key',
key2 : 'a key'
_id: 544034ab914ae3b9270545c1,
__v: 0 } ]
I want to cut key1
and key2
from that objects and want to see this: 我想从该对象中剪切key1
和key2
,并希望看到以下内容:
[ { id: 841,
when: 'date',
who: 'asd',
what: 'what',
_id: 544034ab914ae3b9270545c1,
__v: 0 },
{ id: 841,
when: 'date',
who: 'asd',
what: 'what',
_id: 544034ab914ae3b9270545c1,
__v: 0 } ]
How can I cut that keys and values? 如何削减键和值?
My method is not working. 我的方法不起作用。 (Pseudo) : (伪):
var new_array
for i old_array.length
delete old_array[i].key1
delete old_array[i].key2
new_array.push(old_array[i])
yourArray = yourArray.map(function(current, index, arr){
delete current.key1;
delete current.key2;
return current;
});
That should do what you wish ;-) 那应该做你想要的;-)
Hope that helps, Jan 希望能有所帮助,扬
You pseudo-code is basically what you need, though you can just delete the properties in place: 伪代码基本上是您所需要的,尽管您只需删除适当的属性即可:
var arr = [...]; // your array
for (var i = 0; i < arr.length; i++) {
delete arr[i].key1;
delete arr[i].key2;
}
Like this: 像这样:
function deleteProps(){
var a = Array.slice(arguments) || Array.prototype.slice.call(arguments);
var oa = a[0], dp = a.slice(1);
for(var i=0,l=oa.length; i<l; i++){
for(var n=0,c=dp.length; n<c; n++){
delete oa[i][dp[n]];
}
}
return oa;
}
var newObjectsArray = deleteProps(objectsArray, 'key1', 'key2');
I used underscore.js
我用underscore.js
var cutted_object = _.omit(json_object, "key1", "key2"
"key2", "date",
"id33", "etc");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.