Saving and loading parts of configuration I have a piece of code where I have string pairs (eg. 'a' and 'AA'), where both have their own unique ids. Id for first object is configured by user and second id is received at some point during runtime.

Example code:
http://repl.it/BE5N

Is there smarter way to load/save second ids from/to a file?
It feels somewhat unnecessary to loop through all configs every time new id is found (even if it doesn't happen that often)

And if I added completely new pair-configs during runtime, how should I add them to user's config file while still keeping it human readable?

Initial setup:

var fs = require('fs');

// config pair for first and second
function config (first, second, first_id) {
    this.first    = first;
    this.second   = second;
    // use name as an id for first if none is given
    this.first_id = first_id ? first_id : first;
}


var lookup = function (type, name, arr) {
    return arr.filter(function (obj) {
        return obj[type] === name;
    })[0];
};


// initial config file (few lines out of many)
// user doesn't know second ids
configs = [
    new config('a', 'AA', 'a_id'),
    new config('b', 'BB'),
    new config('c', 'C'),
    new config('d', 'D', 'd_id'),
    new config('e', 'EEE')
];


// read ids for seconds from file if it exists
// read json from file
input_json = {"AA":123,"C":321,"EEE":456};
for (var i=0; i&lt;configs.length; ++i) {
    var key = configs[i].second;
    if (key in input_json) {
        configs[i].second_id = input_json[key];
    }
}


While program is running and new id's are found:

// add new id for 'BB'
BB_config = lookup('second', 'BB', configs);
BB_config.second_id = 654;


// write edited json to file
var output_json = {};
for (var i=0; i&lt;configs.length; ++i) {
    if (configs[i].second_id) {
        output_json[configs[i].second] = configs[i].second_id;
    }
}


console.log(JSON.stringify(output_json));
How about storing your data in sqlite database? All of your operations can be implemented as relational database queries/commands.

Your table will have columns:


FIRST
SECOND
FIRST_ID
SECOND_ID


To perform updates on the second_id based on an object, just use (this is pseudo-code):

for (second in input_json) {
  ...run UPDATE CONFIGS SET SECOND_ID = {input_json[second]} WHERE SECOND = {second} ...


You can make this run efficiently by creating an index on the SECOND column.

Lookup are just SELECT * FROM CONFIGS WHERE ...

For instance, to select only those configs which have second id:

SELECT * FROM CONFIGS WHERE SECOND_ID &lt;&gt; ""


It all depends on how many configs you have. For a large number using a sqlite database could be a win.