0

To clarify the title, I'd like to pass in var 1: var 2 as a key-value pair. However, whenever I do this:

fileobject.add = {var 1: var 2}

where fileobject is the name of the array, using ":" automatically changes filename to text.

The rest of my code:

var FileHasher = require("./FileHasher");

if (process.argv.length < 3) {
  console.log("Program file and one argument are required for this program.");
  process.exit(1);
}

var filename = process.argv[2];
var fs = require('fs');
var data = fs.readFileSync("../sha1.json", "UTF-8"); // read sha1.json contents
var fileobject = JSON.parse(data); // create JS object from file
var file = fs.readFileSync(filename, "UTF-8"); // read file contents

console.log(data); // print all current key-value pairs

fs.createWriteStream("../sha1.json");

var filesha1 = FileHasher(file);
console.log(filesha1);

fileobject.push = (filename: filesha1);
console.log(fileobject);

fs.writeFile("../sha1.json", JSON.stringify(fileobject), function(err) {
  if (err) {
    return console.log(err);
  }
  console.log("data saved");
});

For clarification, FileHasher takes whatever input it's given and returns the sha1.

The other issue I'm having is posting the updated information to sha1.json; my current code just posts the same data I took from it back into it, even though console.log(fileobject) shows "pushed: * sha1 value *" alongside the already imported data, but console.log(data) shows only the information that was read from sha1.json, not any of the information I pushed.

Help with this would be much appreciated!

2 Answers2

3

JavaScript object literals are delimited with {} (U+007B : LEFT CURLY BRACKET / U+007D : RIGHT CURLY BRACKET) not () (U+0028 : LEFT PARENTHESIS / U+0029 : RIGHT PARENTHESIS).

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • I changed my code to reflect that, and now I don't have the ":" error anymore! However, now "filename" automatically changes to a string instead of remaining a variable when I do {filename: filesha1}. –  Jul 15 '15 at 14:53
  • That's a different problem and [a duplicate of this question](http://stackoverflow.com/questions/11043026/variable-as-the-property-name-in-a-javascript-object-literal). – Quentin Jul 15 '15 at 14:59
  • Thanks for the link! I think figured out a rather roundabout way: `var brackets = {}; brackets[filename] = filesha1; fileobject.push = brackets;` On another note, any ideas for why writing to the file only includes the initial data and not the additional things I pushed onto the array? –  Jul 15 '15 at 15:32
  • Scratch that, figured it out! Thanks for your help! –  Jul 15 '15 at 19:13
0

Credit to @Quentin as well for helping me out with this!

I realized that a lot of the answers I found were about putting a property into an object, not an object into an array like I wanted. Therefore, I created my own object using this set of code:

var brackets = {};
brackets[filename] = filesha1;

then pushed it to the array using this:

fileobject.push(brackets);

This is the final code:

var FileHasher = require("./FileHasher");

if (process.argv.length < 3) {
  console.log("Program file and one argument are required for this program.");
  process.exit(1);
}

var filename = process.argv[2];
var fs = require('fs');
var data = fs.readFileSync("../sha1.json", "UTF-8"); // read sha1.json contents
var fileobject = JSON.parse(data); // create JS object from file
var file = fs.readFileSync(filename, "UTF-8"); // read file contents

// console.log(data); // print all current key-value pairs

fs.createWriteStream("../sha1.json"); // be able to write the 

var filesha1 = FileHasher(file);
// console.log(filesha1);

// creating my own object to insert into the array
var brackets = {};
brackets[filename] = filesha1;
fileobject.push(brackets); // insert the object into the array

/*console.log(fileobject[fileobject.length - 1][filename]);
console.log(fileobject);
console.log(typeof(fileobject));*/

fs.writeFile("../sha1.json", JSON.stringify(fileobject), function() {
  console.log("sha1 saved");
});