简体   繁体   English

从JSON对象数组中剪切数据

[英]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: 我想从该对象中剪切key1key2 ,并希望看到以下内容:

[ { 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.

相关问题 从json数据创建自定义对象数组 - create custom objects array from json data 从JSON对象数组中提取3个数据数组 - Extract 3 arrays of data from Array of JSON objects 如何使用javascript将json数据格式从数组更改为对象数组? - How to change json data format from array to array of objects with javascript? 如何访问数组位置内几个json对象的数据 - How to acces to the data from several json objects inside an array position Javascript 嵌套 for 循环显示来自 JSON 对象数组的数据 - Javascript nested for loop displaying data from JSON objects array 遍历数组中的对象,显示JSON中的数据 - Looping over objects in array, display data from JSON 从大型json数据集创建对象数组 - Creating array of objects from large json data set 如何从包含多个对象数组的Json数据中提取多个对象数组? - How to extract multiple array of objects from Json data containing multiple array of objects? 如何处理对象数组? 我有一个包含3个对象的数组,这些对象包含来自数据库的JSON数据 - How to deal with array of objects? I have an array with 3 objects containing JSON data from my db 从另一个对象数组[配置]中的对象数组[表单]访问JSON数据检查下面的Json数据 - Accessing JSON data from array of objects [form] who is inside another array of objects [configuration] check the Json Data below
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM