I'm trying to select only certain values from my object to write into a file. But this writes the whole object and also unless I use util.inspect it just writes as objects. This should write the values I have chosen from the objects line by line as they come in:
var objectsToFile = function(objectsTotal){
objectsTotal = _.values(objectsTotal, function(value) {
return value.objectTo.employeeName;
});
objectsTotal = _.values(objectsTotal, function(value) {
return value.employeeCurrent;
});
objectsTotal = _.values(objectsTotal, function(value) {
return value.employeePast;
});
writeFile('objectsTotalet.csv', util.inspect(objectsTotal), function(err) {
if (err) console.log(err);
});
};
objectsTotal comes through the function like:
[ { objectTo:
{ employeeName: 'John',
employeeID: '234234',
DOB: '2333'},
employeeCurrent: true,
employeePast: false},
{ objectTo:
{ employeeName: 'Janet',
employeeID: '23423432',
DOB: '23333' },
employeeCurrent:true,
employeePast: false} ]
The output will be conditional which is why I'm using the underscore library but it doesn't work it doesn't even use the returned values from underscore:
There will be other values added to the object so will also need to add a return value like:
objectsTotal = _.values(objectsTotal, function(value) {
return value.employeeStatus != 'employed' ||
value.url.indexOf('employee:') === -1 ||
value.employeeid.length === ('id:')
});
I do need to use this library to give the results I want in a csv file. The output will look like the objectsTotal values returned line by line and can be in one cell.
Well your underscore code doesn't seem to work because it doesn't return any result in your _.values()
calls.
I think you are mis-using the _.values()
method from the _.values()
underscore Docs we can see that it doesn't have a second argument for a callback
, so I think the code in this callback will never be executed.
Solution:
Anyway this can be achieved with pure JavaScript code:
var filteredObjects = objectsTotal.filter(function(value) {
return value.objectTo.employeeStatus != 'employed';
}).map(function(obj) {
return {
employeeName: obj.objectTo.employeeName,
employeeCurrent: obj.employeeCurrent,
employeePast: obj.employeePast
}
}).map(function(o) {
return Object.values(o);
});
Explanation:
.filter()
method to filter the objects having the specified conditions, I used only one test but you can implement all your tests, it will give us an array
with all the objects matching these conditions..map()
method to get only employeeName
, employeeCurrent
and employeePast
properties of each object
..map()
to get only the values of these propeties using Object.values()
in the callback function.After filtering your array
of objects, you can transform it to a string
using .reduce()
method like this:
var filteredResult = filteredObjects.reduce(function(acc, currentValue) {
return accumulator + currentValue.join(" \n");
}).join(" ,");
Then write the content of filteredResult
string variable to your csv
file:
writeFile('objectsTotalet.csv', filteredResult, function(err) {
if (err) console.log(err);
});
Demo:
var objectsTotal = [{
objectTo: {
employeeName: 'John',
employeeID: '234234',
DOB: '2333',
employeeStatus: 'employed'
},
employeeCurrent: true,
employeePast: false
},
{
objectTo: {
employeeName: 'Janet',
employeeID: '23423432',
DOB: '23333',
employeeStatus: 'unemployed'
},
employeeCurrent: true,
employeePast: false
}
];
var filteredObjects = objectsTotal.filter(function(value) {
return value.objectTo.employeeStatus != 'employed';
}).map(function(obj) {
return {
employeeName: obj.objectTo.employeeName,
employeeCurrent: obj.employeeCurrent,
employeePast: obj.employeePast
}
}).map(function(o) {
return Object.values(o);
});
var filteredResult = filteredObjects.reduce(function(acc, currentValue) {
return accumulator + currentValue.join(" \n");
}).join(" ,");
console.log(filteredResult);
<script src="http://underscorejs.org/underscore-min.js"></script>
<script data-require="lodash.js@*" data-semver="4.17.4" src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
Well, I realized what you want to achieve in a plunker using lodash with a chain of multiple functions, this is the code used :
// First, do your filtering conditions here
var game = data.filter(g => {
return g.debit === true;
});
// Then, pick the wanted attributes here
game = _.map(game, (g) => {
var o = _.pick(g, ['amount', 'debit']);
o = Object.values(o);
return o;
});
// Finally merge your arrays here
var merged = [].concat.apply([], game);
For printing your array into csv, you just have to use the final merge array in your printing function.
Here's the plunker link: http://plnkr.co/edit/uvH4Ww4HMaQQVXQCBVKP
I hope that I answered your question as needed.
PS: I can explain every block of code which isn't clear.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With