I have an array that lists all users, and each new connection gets added to this array along with the file name they uploaded. An example after two people have uploaded a file each is:
[ '{ 127.0.2.2: test.pdf}', '{ 127.0.0.1: asf.pdf}' ]
How would I return the file name with just the IP? If needed, I can split the file name later if returning the IP + file name works.
I've tried:
console.log(_.findWhere(userlist, function(){ var tempObj = {}; return tempObj[user] = fileName;}));
But this just returns the first value, every time. Using the example, it would return {127.0.2.2: test.pdf} every time. user and fileName are defined after someone uploads, so they should overwrite with the current information (which is proven right, as it adds it correctly), but it still just returns the first key/value pair every time.
Let me know if this makes sense.
As Brennan stated in the comments you should use _.find instead of your current code.
var userlist = [ { '127.0.2.2': 'test.pdf'}, { '127.0.0.1': 'asf.pdf'} ];
var ip = '127.0.0.1';
var userWithIp = _.find(userlist, function(f){ return Object.keys(f)[0] === ip;})
Here is working code using the _.find method: