通过reduce键创建新的json对象

时间:2018-05-18 14:16:44

标签: javascript json methods

我已经搜索了堆栈溢出大约30分钟,但我不确定我是否可以使用filter,map或reduce。我有以下JSON

输入

[
  {
    "_id": "5a3711070776d02ed87d2100",
    "index": 0,
    "guid": "fb08e9c3-c5b6-44e9-a4c6-73edcfbc8ae9",
    "isActive": false,
    "balance": "$2,762.54",
    "picture": "http://placehold.it/32x32",
    "age": 39,
    "eyeColor": "brown",
    "name": "Crystal Sampson",
    "gender": "female",
    "company": "ZAGGLE",
    "email": "crystalsampson@zaggle.com",
    "phone": "+1 (822) 582-2186",
    "address": "836 Batchelder Street, Harviell, Indiana, 966",
    "about": "Duis eu irure mollit sit voluptate proident do reprehenderit irure sunt irure tempor. Elit commodo mollit Lorem esse elit ea nostrud. Commodo cillum ipsum enim incididunt aliquip consectetur cillum cillum commodo et aliquip.\r\n",
    "registered": "2017-01-31T09:56:39 -07:00",
    "latitude": 18.386331,
    "longitude": -3.807078,
    "tags": [
      "laboris",
      "reprehenderit",
      "aliqua",
      "ipsum",
      "in",
      "consequat",
      "anim"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Lily Mullen"
      },
      {
        "id": 1,
        "name": "Smith Howard"
      },
      {
        "id": 2,
        "name": "Ronda Stafford"
      }
    ],
    "greeting": "Hello, Crystal Sampson! You have 2 unread messages.",
    "favoriteFruit": "banana"
  }]

输出

[{"name": "Crystal Sampson",
"gender": "female",
"company": "ZAGGLE",
"email": "crystalsampson@zaggle.com"}]

我想减少我感兴趣的关键。非常感谢你。

5 个答案:

答案 0 :(得分:0)

您可以对外部数组中的对象使用简单的for循环,然后为对象的键检查是否存在要考虑的selectedKey数组上的密钥。使用selectedKey可以帮助您轻松扩展所需的任何键,只需在selectedKey数组中指定它们。



var data = [
  {
    "_id": "5a3711070776d02ed87d2100",
    "index": 0,
    "guid": "fb08e9c3-c5b6-44e9-a4c6-73edcfbc8ae9",
    "isActive": false,
    "balance": "$2,762.54",
    "picture": "http://placehold.it/32x32",
    "age": 39,
    "eyeColor": "brown",
    "name": "Crystal Sampson",
    "gender": "female",
    "company": "ZAGGLE",
    "email": "crystalsampson@zaggle.com",
    "phone": "+1 (822) 582-2186",
    "address": "836 Batchelder Street, Harviell, Indiana, 966",
    "about": "Duis eu irure mollit sit voluptate proident do reprehenderit irure sunt irure tempor. Elit commodo mollit Lorem esse elit ea nostrud. Commodo cillum ipsum enim incididunt aliquip consectetur cillum cillum commodo et aliquip.\r\n",
    "registered": "2017-01-31T09:56:39 -07:00",
    "latitude": 18.386331,
    "longitude": -3.807078,
    "tags": [
      "laboris",
      "reprehenderit",
      "aliqua",
      "ipsum",
      "in",
      "consequat",
      "anim"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Lily Mullen"
      },
      {
        "id": 1,
        "name": "Smith Howard"
      },
      {
        "id": 2,
        "name": "Ronda Stafford"
      }
    ],
    "greeting": "Hello, Crystal Sampson! You have 2 unread messages.",
    "favoriteFruit": "banana"
  }];
  
var selectedKey = ['name','company','gender', 'email'];

var res = [];
data.forEach((obj)=>{
  Object.keys(obj).forEach((key)=>{
    if(selectedKey.indexOf(key) !== -1){
      var tempObj = {};
      tempObj[key] = obj[key]
      res.push(tempObj);
    }
  });
});

console.log(res);




答案 1 :(得分:0)

您想要做的是.map在数组上,并且仅使用您感兴趣的键创建新对象。



const input = [
  {
    "_id": "5a3711070776d02ed87d2100",
    "index": 0,
    "guid": "fb08e9c3-c5b6-44e9-a4c6-73edcfbc8ae9",
    "isActive": false,
    "balance": "$2,762.54",
    "picture": "http://placehold.it/32x32",
    "age": 39,
    "eyeColor": "brown",
    "name": "Crystal Sampson",
    "gender": "female",
    "company": "ZAGGLE",
    "email": "crystalsampson@zaggle.com",
    "phone": "+1 (822) 582-2186",
    "address": "836 Batchelder Street, Harviell, Indiana, 966",
    "about": "Duis eu irure mollit sit voluptate proident do reprehenderit irure sunt irure tempor. Elit commodo mollit Lorem esse elit ea nostrud. Commodo cillum ipsum enim incididunt aliquip consectetur cillum cillum commodo et aliquip.\r\n",
    "registered": "2017-01-31T09:56:39 -07:00",
    "latitude": 18.386331,
    "longitude": -3.807078,
    "tags": [
      "laboris",
      "reprehenderit",
      "aliqua",
      "ipsum",
      "in",
      "consequat",
      "anim"
    ],
    "friends": [
      {
        "id": 0,
        "name": "Lily Mullen"
      },
      {
        "id": 1,
        "name": "Smith Howard"
      },
      {
        "id": 2,
        "name": "Ronda Stafford"
      }
    ],
    "greeting": "Hello, Crystal Sampson! You have 2 unread messages.",
    "favoriteFruit": "banana"
  }];
  
  const output = input.map(element => {
    return {
      name: element.name,
      gender: element.gender,
      company: element.company,
      email: element.email
    }
  });
  
  console.log(output);




答案 2 :(得分:0)

你可以这样做一些过滤器:



var input = [{
  "_id": "5a3711070776d02ed87d2100",
  "index": 0,
  "guid": "fb08e9c3-c5b6-44e9-a4c6-73edcfbc8ae9",
  "isActive": false,
  "balance": "$2,762.54",
  "picture": "http://placehold.it/32x32",
  "age": 39,
  "eyeColor": "brown",
  "name": "Crystal Sampson",
  "gender": "female",
  "company": "ZAGGLE",
  "email": "crystalsampson@zaggle.com",
  "phone": "+1 (822) 582-2186",
  "address": "836 Batchelder Street, Harviell, Indiana, 966",
  "about": "Duis eu irure mollit sit voluptate proident do reprehenderit irure sunt irure tempor. Elit commodo mollit Lorem esse elit ea nostrud. Commodo cillum ipsum enim incididunt aliquip consectetur cillum cillum commodo et aliquip.\r\n",
  "registered": "2017-01-31T09:56:39 -07:00",
  "latitude": 18.386331,
  "longitude": -3.807078,
  "tags": [
    "laboris",
    "reprehenderit",
    "aliqua",
    "ipsum",
    "in",
    "consequat",
    "anim"
  ],
  "friends": [{
      "id": 0,
      "name": "Lily Mullen"
    },
    {
      "id": 1,
      "name": "Smith Howard"
    },
    {
      "id": 2,
      "name": "Ronda Stafford"
    }
  ],
  "greeting": "Hello, Crystal Sampson! You have 2 unread messages.",
  "favoriteFruit": "banana"
}];
console.log(input.map(function (item) {
  return {
    name: item.name,
    gender: item.gender,
    company: item.company,
    email: item.email
  };
}));




答案 3 :(得分:0)

对于你的问题,我使用map函数迭代你的数组。

谷歌更多关于地图,过滤器和减少之间的区别。你会得到很棒的例子。并自己尝试。

var arrResult = [];

var InputObject = [{
"_id": "5a3711070776d02ed87d2100",
"index": 0,
"guid": "fb08e9c3-c5b6-44e9-a4c6-73edcfbc8ae9",
"isActive": false,
"balance": "$2,762.54",
"picture": "http://placehold.it/32x32",
"age": 39,
"eyeColor": "brown",
"name": "Crystal Sampson",
"gender": "female",
"company": "ZAGGLE",
"email": "crystalsampson@zaggle.com",
"phone": "+1 (822) 582-2186",
"address": "836 Batchelder Street, Harviell, Indiana, 966",
"about": "Duis eu irure mollit sit voluptate proident do reprehenderit irure sunt irure tempor. Elit commodo mollit Lorem esse elit ea nostrud. Commodo cillum ipsum enim incididunt aliquip consectetur cillum cillum commodo et aliquip.\r\n",
"registered": "2017-01-31T09:56:39 -07:00",
"latitude": 18.386331,
"longitude": -3.807078,
"tags": [
  "laboris",
  "reprehenderit",
  "aliqua",
  "ipsum",
  "in",
  "consequat",
  "anim"
],
"friends": [
  {
    "id": 0,
    "name": "Lily Mullen"
  },
  {
    "id": 1,
    "name": "Smith Howard"
  },
  {
    "id": 2,
    "name": "Ronda Stafford"
  }
],
"greeting": "Hello, Crystal Sampson! You have 2 unread messages.",
"favoriteFruit": "banana"}];


InputObject.map((x)=>{
    var obj = {
        "name": x.name,
        "gender": x.gender,
        "company": x.company,
        "email": x.email
    };
    arrResult.push(obj);
});

console.log(arrResult);

答案 4 :(得分:0)

MongoDB中的文档是否真的来自?为什么不使用projections

如果不想使用投影你有另一种选择,请记住,JSON是一个对象,所以:

var obj = new Object(); 
obj['XKey1'] = "Value1";
obj['XKey2'] = "Value2";
obj['YKey3'] = "Value3";
obj['YKey4'] = "Value4";


Object.keys(obj).map(function(objectKey, index) {
  if(objectKey=='YKey3') delete obj[objectKey];
});

console.log(obj)