2

I have a file called contentful.json which is stored in the root of my directory.

This is what that file looks like:

[{ "URL": "/force", "SpaceID": "2g1335I9GYQCKwmQYCuSSI" }, { "URL": "/heroku", "SpaceID": "2g1335I9GYQCKwmQYCuSSI" }, { "URL": "/slack", "SpaceID": "2g1335I9GYQCKwmQYCuSSI" }, { "URL": "/contentful", "SpaceID": "2g1335I9GYQCKwmQYCuSSI" }, { "URL": "/openShift", "SpaceID": "2g1335I9GYQCKwmQYCuSSI" }]

I then have a file called contentful.js.

Which looks like this:

const fs = require('fs');

let obj;

fs.readFile('./contentful.json', 'utf8', (err, jsonData) => {
  if (err) throw err;
  obj = JSON.parse(jsonData);

  // Method One
  for (let i = 0, l = obj.length; i < l; i++) {
    console.log(obj.URL);
    // ...
  }

  // Method Two
  Object.keys(obj).forEach((key) => {
    if (key.url === '/heroku') {
      console.log(key.SpaceID);
    }
  });
});

What I am trying to do is read the contentful.json file from the contentful.js file. Which works.

Then I am trying to loop through that file and find a specific value.

E.G loop through the JSON object, and where URL equals a dynamic value (/force) return me the SpaceID.

So this JSON object could be n big. I have attempted two solutions but don't think I am quite their yet.

I need to input a URL into the contnetful.js file and if that URL matches the URL in the contnetful.json file I would like it to return that SpaceID.

1
  • the output of console.log(obj.URL) is undefined. It prints undefined out as many time as the size of the JSON object. In this case 5. However for Method Two console.log(key.SpaceID); it doesn't print anything out. Commented Jan 11, 2017 at 9:39

3 Answers 3

3

Change method one from this:

  // Method One
  for (let i = 0, l = obj.length; i < l; i++) {
    console.log(obj.URL);
    // ...
  }

to this:

  // Method One
  for (let i = 0, l = obj.length; i < l; i++) {
    console.log(obj[i].URL);
    if (obj[i].URL === someVariable) {
        console.log("found match for ", someVariable);
        console.log("SpaceID = ", obj[i].SpaceID);
    }
    // ...
  }

You have to use the index in your for loop to index into the obj array to get the object at that spot in the array.

If you want the URL you are looking for to be in a variable, then just use a variable in your comparison. You can wrap the whole thing in a function and pass the desired string into the function as an argument to the function and then you can reuse the function to match different values.

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

3 Comments

still no dynamic URL.
@DominikNoll - Change "/force" to a variable if you want.
Thats the answer to his question :-)
3

You can use filter to check for the dynamic URL. It will return the filtered matches of a particular URL. You can get the spaceID then from the filtered result.

let obj;

let dynamicURL = "/heroku"

obj = [{
  "URL": "/force",
  "SpaceID": "2g1335I9GYQCKwmQYCuSSI"
}, {
  "URL": "/heroku",
  "SpaceID": "2g1335I9GYQCKwmQYCuSSI"
}, {
  "URL": "/slack",
  "SpaceID": "2g1335I9GYQCKwmQYCuSSI"
}, {
  "URL": "/contentful",
  "SpaceID": "2g1335I9GYQCKwmQYCuSSI"
}, {
  "URL": "/openShift",
  "SpaceID": "2g1335I9GYQCKwmQYCuSSI"
}]

var filtered = obj.filter(function(elem){
 return (elem.URL == dynamicURL)
});

console.log(filtered[0].SpaceID);

Comments

1
//Method One:
function searchForUrl(obj, url){
    for(let i = 0, l = obj.length; i < l; i++){
        if(obj[i].Url == url){
            //DO STUFF
            break; //better performance, only if the URL is unique!!
        }
    }
}

//Method Two:
function searchForUrl(obj, url){
    Object.keys(obj).forEach((key) => {
    if (key.url === url) {
      console.log(key.SpaceID);
    }
});

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.