0

I have a JSON object like this

myObj = {
"knowncount": [{
    "id": "planet",
    "knownCount": 8,
    "updateDate": "24/08/2006",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/planet"
}, {
    "id": "dwarfPlanet",
    "knownCount": 5,
    "updateDate": "24/08/2006",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/dwarfPlanet"
}, {
    "id": "asteroid",
    "knownCount": 1027022,
    "updateDate": "11/11/2020",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/asteroid"
}, {
    "id": "comet",
    "knownCount": 3690,
    "updateDate": "11/11/2020",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/comet"
}]}

How can I search my object to find where the key is 'id' and then print its value for knownCount?

Example. If 'id' == 'planet'

// 8

I've tried to covert to array to use array functions like array.find() but this doesn't seem to work. I check type with typeof and continue to get type object. What is the correct way to change to array or is there a comparable way to do this with objects or must I iterate over the object and create a set or something similar?

9

4 Answers 4

0
myObj
 .knowncount
 .find(({ id }) => id === 'planet')
 .knownCount;
Sign up to request clarification or add additional context in comments.

1 Comment

If desired id value is never met, this code will throw Referrence Error
0

Try it

const res = myObj.knowncount.find(el => el.id === 'planet')?.knownCount || 'Planet not found';
console.log(res);

You can't use finddirectly on the myObj because it's an Object, and not an Array.

myObj = {
"knowncount": [{
    "id": "planet",
    "knownCount": 8,
    "updateDate": "24/08/2006",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/planet"
}, {
    "id": "dwarfPlanet",
    "knownCount": 5,
    "updateDate": "24/08/2006",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/dwarfPlanet"
}, {
    "id": "asteroid",
    "knownCount": 1027022,
    "updateDate": "11/11/2020",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/asteroid"
}, {
    "id": "comet",
    "knownCount": 3690,
    "updateDate": "11/11/2020",
    "rel": "https://api.le-systeme-solaire.net/rest/knowncount/comet"
}]}

const res1 = myObj.knowncount.find(el => el.id === 'planet')?.knownCount || 'Planet not found';
console.log('Know planet count:', res1);
const res2 = myObj.knowncount.find(el => el.id === 'planetX')?.knownCount || 'Planet not found';
console.log('Unknown planet count:', res2);

7 Comments

This code will throw Referrence Error if id value you search for is never found.
What you have to show if the planet isnt found?
Personally, I don't need anything. I just pointed out that solution failing with error when desired result is not found (which may be pretty common thing) is not that much robust.
I updated my answer, try it with a unknown planet please
That would've been much easier to try if you would make it executable snippet.
|
0

You could just loop through the object and test fo the id:

function getId(myId){
let a =myObj.knowncount
for (el in a){
   if(el.id==myId)
return el
}
}

3 Comments

Why do you think so?
Do you realize that I (just like everyone else) can see you fixed your mistake after my comment?
And back to the 'why'-question, you might find the following quick test I've set up for you to actually prove your solution still won't work. Feel free to use it for getting your code valid.
0

Use this getKnownCount function to get the known count.

function getKnownCount(Id) {
    let knownCountArray = myObj.knowncount;
    for (let i = 0; i < knownCountArray.length; i++) {
      if ((knownCountArray[i].id == Id)) {
        return knownCountArray[i].knownCount;
      }
    }
    return "Not found";
  }

As,

getKnownCount("planet")

or you can just simply search "planet" via the find method.

myObj.knowncount.find(el => el.id == 'planet')?.knownCount || 'Not found';

9 Comments

Your second snippet will return Not found for {id: 'planet', knownCount: 0}
@YevgenGorbunkov I test it, it works fine for me can you describe more clearly what is not working for you.
'> it works', no, it doesn't, because it may never go further the first item, if you would have actually tested that you would figure that out
@YevgenGorbunkov yeah, there is some issue I fixed it, now it work.
I think you forget to accept this answer if you don't get the answer let me know.
|

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.