-1

I received the below JSON and having difficulty retrieving the value of jersey_num.

const json = [{
    $: {
        Type: "first_name"
    },
    _: "Evan"
}, {
    $: {
        Type: "last_name"
    },
    _: "Ferguson"
}, {
    $: {
        Type: "birth_date"
    },
    _: "2004-10-19"
}, {
    $: {
        Type: "weight"
    },
    _: "Unknown"
}, {
    $: {
        Type: "height"
    },
    _: "Unknown"
}, {
    $: {
        Type: "jersey_num"
    },
    _: "28"
}, {
    $: {
        Type: "real_position"
    },
    _: "Striker"
}, {
    $: {
        Type: "real_position_side"
    },
    _: "Centre"
}, {
    $: {
        Type: "join_date"
    },
    _: "2021-08-23"
}, {
    $: {
        Type: "country"
    },
    _: "Republic of Ireland"
}]

I tried using the below code but received undefined

const jersey = Object.entries(json).find(([, e]) => Object.values(e).includes('jersey_num'))
console.log(jersey)

I'm pretty sure there is something wrong with the above code. Hope someone can help and guide me on how to retrieving the jersey_num value

EDIT

  1. Edited the JSON object to the proper format
  2. The value that I want to retrieve is '28' as in Type: "jersey_num"
  • 2
    Is `json` an array? Your example structure is missing `[]` if so – Nick Parsons Nov 01 '22 at 10:19
  • 1
    Given that he's trying to use Object.entries, I think it's not. Thus, I think the problem is in the format of the original object. – Antti_M Nov 01 '22 at 10:20
  • Does the `jersey_num` always sit under an object at the `'$'` key and the value of the `'Type'` key? – Nick Parsons Nov 01 '22 at 10:21
  • 2
    you have added an invalid json here please paste the valid json and the desired output. Here Json is neither an object nor and array. – jitender Nov 01 '22 at 10:23
  • _" retrieving the value of jersey_num."_ - What do you mean by the value of jersey_num? In your example, `jersey_num` is already a _value_. What is your expected output (is it 28, the whole object, or something else)? – Nick Parsons Nov 01 '22 at 10:24
  • 2
    There isn't any trace of JSON in this question, just a JavaScript array that contains some JavaScript objects. [JSON](https://json.org) is a text representation of some data structure. `json` is a JavaScript array represented as source code, not JSON. – axiac Nov 01 '22 at 11:04

1 Answers1

2

You're grabbing the entries of json, which is an array. Typically you don't need to do that as the entries of an array are its indexes and values, which you already have access to through standard iterations methods, such as a for loop, or the many array methods such as .forEach(), .map(), .find() etc. Instead, you can call .find() directly on json, as it's already an array, and then return true when the object's $.Type is equal to jersey_num. Once you have the object, you can use ?._ to safely access your value:

const json = [{ "_": "Evan", "$": { "Type": "first_name" } }, { "_": "Ferguson", "$": { "Type": "last_name" } }, { "_": "2004-10-19", "$": { "Type": "birth_date" } }, { "_": "Unknown", "$": { "Type": "weight" } }, { "_": "Unknown", "$": { "Type": "height" } }, { "_": "28", "$": { "Type": "jersey_num" } }, { "_": "Striker", "$": { "Type": "real_position" } }, { "_": "Centre", "$": { "Type": "real_position_side" } }, { "_": "2021-08-23", "$": { "Type": "join_date" } }, { "_": "Republic of Ireland", "$": { "Type": "country" } } ];

const obj = json.find(obj => obj.$.Type === "jersey_num");
console.log(obj?._);

Here ?. is optional chaining, which means that if .find() were to not find your object within your array for some reason, then obj will be undefined. Trying to access properties on undefined with undefined._ will throw an error, however, with optional chaining: undefined?._ JS will no longer throw an error and instead will returned undefined.

Nick Parsons
  • 45,728
  • 6
  • 46
  • 64