0

I'm trying to iterate a json object with nodejs and it's turning out to be a real challenge.

> var fs = require('fs');
> var meta = JSON.parse(fs.readFileSync('path_to_file'));
> typeof meta
'object'
> for ( k in meta)  console.log(k);
key1
key2
> meta.map(function(v,k) { console.log(k +'=' + v); })
TypeError: Object #<Object> has no method 'map'
    at repl:1:7
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.EventEmitter.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.EventEmitter.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> 

I'm new to node, is this normal ?

0

1 Answer 1

1

map is for arrays, you are applying it to an object.

You can try _.map of underscore.js

var _ = require('underscore');
_.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
=> [3, 6, 9]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, that's stupid of me. I've tried lodash var _ = require('lodash'); _.map(meta, function(k,v) ... ) and got TypeError: Cannot call method 'map' of undefined... whats the problem man ?
To require a npm module, you have to install it first. Run the command npm install lodash --save in your working directory.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.