Node WriteFile does not write objects I need to use underscore. Records all objects

I am trying to select only certain values ​​from my object for writing to a file. But this writes the whole object, and also, if I do not use util.inspect, it just writes as objects. This should record the values ​​that I selected from the objects in turn when they enter:

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 a 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, so I use the underscore library, but it does not work, it does not even use the return values ​​from the underscore:

Other values ​​will be added to the object, so you also need to add the return value, for example:

 objectsTotal = _.values(objectsTotal, function(value) { return value.employeeStatus != 'employed' || value.url.indexOf('employee:') === -1 || value.employeeid.length === ('id:') }); 

I need to use this library to provide the results that I want in a csv file. The result will look like objects. The returned values ​​are in turn and can be in the same cell.

+7
json javascript object
source share
2 answers

It's good that your underscore code does not work, because it does not return any results in your _.values() calls.

I think you are using the _.values() method from _.values() underscore Docs incorrectly. can see that it has no second argument for callback , so I think the code in this callback will never be executed.

Decision:

In any case, this can be achieved using 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:

  • We use the .filter() method to filter objects that have given conditions, I used only one test, but you can implement all your tests, it will give us an array with all the objects that meet these conditions.
  • Then we use .map() to get only the employeeName , employeeCurrent and employeePast properties of each object .
  • Then, recall .map() to get only the values ​​of these propositions using Object.values() in the callback function.

After filtering the array objects, you can convert it to string using the .reduce() method as follows:

 var filteredResult = filteredObjects.reduce(function(acc, currentValue) { return accumulator + currentValue.join(" \n"); }).join(" ,"); 

Then write the contents of the filteredResult string variable to the 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/ lodash@4.17.4 /lodash.min.js"></script> 
+2
source share

Well, I realized what you want to achieve in the plunter using lodash with a chain of several 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); 

To print your array in csv you just need to use the final merge array in your print function. A.

Here's the plunker link: http://plnkr.co/edit/uvH4Ww4HMaQQVXQCBVKP

I hope that I answered your question as necessary.

PS: I can explain every block of code that is not clear.

+1
source share

All Articles