admin管理员组

文章数量:1325374

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.

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.

Share Improve this question asked Jan 11, 2017 at 9:31 user3180997user3180997 1,9263 gold badges21 silver badges37 bronze badges 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. – user3180997 Commented Jan 11, 2017 at 9:39
Add a ment  | 

3 Answers 3

Reset to default 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 parison. 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.

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);

//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);
    }
});

本文标签: arraysJavaScriptLoop through Object to find Specific Key ValueStack Overflow