admin管理员组文章数量:1326112
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.
-
the output of
console.log(obj.URL)
isundefined
. It printsundefined
out as many time as the size of the JSON object. In this case 5. However for Method Twoconsole.log(key.SpaceID);
it doesn't print anything out. – user3180997 Commented Jan 11, 2017 at 9:39
3 Answers
Reset to default 3Change 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
版权声明:本文标题:arrays - JavaScript - Loop through Object to find Specific Key Value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742192552a2430468.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论