Reputation:
I have searched stack overflow for about 30 minutes but I am not sure that I can use filter, map or reduce. I have the following JSON
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": "[email protected]",
"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"
}]
Output
[{"name": "Crystal Sampson",
"gender": "female",
"company": "ZAGGLE",
"email": "[email protected]"}]
I want to reduce key that I interest. Thank you so much.
Upvotes: 0
Views: 1207
Reputation: 81
Is a document from collection in MongoDB really?? Why not use projections?
If don't want use projections you have another alternative, remember, a JSON is a Object, so:
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)
Upvotes: 0
Reputation: 1361
For your question, i have used map function to iterate over your array.
Google more about difference between map, filter and reduce. You'll get fantastic examples. And try them on your own.
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": "[email protected]",
"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);
Upvotes: 0
Reputation: 167182
You can do some sort of filter like this:
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": "[email protected]",
"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
};
}));
Upvotes: 0
Reputation: 2367
What you want to do is .map
over the array and make new objects only with the keys you are interested in.
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": "[email protected]",
"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);
Upvotes: 1
Reputation: 30739
You can use a simple for loop for the object in the outer array then for the keys of the object to check if the key exist on the selectedKey
array which you want to consider. Using selectedKey
will help you to scale easily for any of the keys you want, just specify them in selectedKey
array.
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": "[email protected]",
"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);
Upvotes: 0