0

I have an object from osm search. And, the tags are like below.

 a: {   
    tg: [
      ["leisure", "stadium"],
      ["name", "AA Stadium"],
      ["key", "game"],
    ]
   }

 b: {   
    tg: [
      ["key", "game"],
    ]
   }

c: {   
    tg: [
      ["name", "GTA"],
      ["key", "game"],
    ]
   }

Some do not have the name. Therefore, first, I want to check if name exits and then if it exists, returns the index of its array in order to get the name. I can get the name value with the index, but I am stuck at getting index number of the array that contains the name.

How can I get the index of the array that contains name value in this case?

1 Answer 1

1

you can use Object.fromEntries()

const a = { tg: [ [ 'leisure', 'stadium'] , [ 'name', 'AA Stadium'] , [ 'key', 'game'] ] } 
    , b = { tg: [ [ 'key', 'game'] ] } 
    , c = { tg: [ [ 'name', 'GTA'] , [ 'key', 'game'] ] } 


let aTG =  Object.fromEntries(a.tg)
  , bTG =  Object.fromEntries(b.tg)
  , cTG =  Object.fromEntries(b.tg)
  
console.log( 'aTG asName ?', Boolean(aTG.name) )
console.log( 'bTG asName ?', Boolean(bTG.name) )
console.log( 'cTG asName ?', Boolean(cTG.name) )

if (!!aTG.name) console.log( 'aTG.key ->', aTG.key )
 

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

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.