0

I have the following node.js file:

[
  {
    "name":"Apple inc",
    "symbol":"AAPL",
    "logo":"apple.png",    
    "price":123,
    "prod":"Apple inc, mac, macbook, iphone, ipod, ipad, osx"
  },
  {
    "name":"Nvidia Corporation",
    "symbol":"NVDA",  
    "logo":"nvidia.png",
    "price":321,
    "prod":"Nvidia Corporation, gforce, g-force, shield"
  },
  {
    "name":"Google inc",
    "symbol":"GOOG", 
    "logo":"google.png",
    "price":321,
    "prod":"search, android, glass, drive, code school"
  }  
]

How can i access each object inside without the the index of the array, using only the symbol. Today i have a for loop that is running through all the array:

var fs = require('fs');

var stocks = JSON.parse(fs.readFileSync("stocks.json"));
for (var i=0; i<stocks.length; i++) {
    if (stocks[i].symbol==="GOOG") {
        console.log(i+ " ." , stocks[i] );

}

If i'll change the Json file to this format :

{ "APPL":
  {
    "name":"Apple inc",
    "symbol":"AAPL",
    "logo":"apple.png",    
    "price":123,
    "prod":"Apple inc, mac, macbook, iphone, ipod, ipad, osx"
  },
  "NVDA":
  {
    "name":"Nvidia Corporation",
    "symbol":"NVDA",  
    "logo":"nvidia.png",
    "price":321,
    "prod":"Nvidia Corporation, gforce, g-force, shield"
  },
  "GOOG":
  {
    "name":"Google inc",
    "symbol":"GOOG", 
    "logo":"google.png",
    "price":321,
    "prod":"search, android, glass, drive, code school"
  }  
}

2 Answers 2

1

you can use a simple for loop to iterate through

var obj = { "APPL":
  {
    "name":"Apple inc",
    "symbol":"AAPL",
    "logo":"apple.png",    
    "price":123,
    "prod":"Apple inc, mac, macbook, iphone, ipod, ipad, osx"
  },
  "NVDA":
  {
    "name":"Nvidia Corporation",
    "symbol":"NVDA",  
    "logo":"nvidia.png",
    "price":321,
    "prod":"Nvidia Corporation, gforce, g-force, shield"
  },
  "GOOG":
  {
    "name":"Google inc",
    "symbol":"GOOG", 
    "logo":"google.png",
    "price":321,
    "prod":"search, android, glass, drive, code school"
  }  
};

for(var i=0, keys = Object.keys(obj),len=keys.length; i <len ; i++){
  if(obj[keys[i]].symbol === "GOOG"){
    console.log(i +'.' +obj[keys[i]]);
  }
}

Object.keys(obj) will return ["APPL", "NVDA", "GOOG"]


to check if GOOG exists, you can do this

var result = Object.keys(obj).indexOf('GOOG') > -1;
console.log(result); // will be true

DEMO

Sign up to request clarification or add additional context in comments.

2 Comments

How can i check if the key is existing in stocks for example i want to check if IBM is there, should i just do t like this : if (stocks["IBM"!=null) another thing once i've updated the stocks how can i write it back to the file?
keys is an array, so if you want to check a item exists in it.you could do keys.indexOf('IBM') > -1
1

If I'm understanding correctly, you'll be using the second format going forward?

That makes it as simple as :

var stocks = JSON.parse(fs.readFileSync("stocks.json"));
var goog = stocks.GOOG;

console.log(goog);

Otherwise you could query the original array if you wanted to:

var stocks = JSON.parse(fs.readFileSync("stocks.json"));
var goog = stocks.filter(function(stock) { if(stock.symbol == 'AAPL') return true; } );

console.log(goog[0]);

PS. don't use readFileSync it's really bad practice. ;)

Comments

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.