mardi 25 mai 2021

Looping over an array while using template literals

I have a variable in which i execute a query once the user fires the onselect function and highlights a room in the application, i want the value coming after space_ to be dynamic for the initial query variable. each room in the interface has a unique guid assigned to it. So if the user clicks on room 128 in the interface it should pass in the id with a value of 892 into the initial query. is it possible to use a for loop to do this if so how can this be done? or should a different approach be taken

const value = {
    room128: {
               id: 892, 
               guid: '0KLkXPBfvES9D1y7Ejijkm', 
             },
    room127: {
                id: 1023, 
                guid: '0KLkXPBfvES9D1y7EjijkC', 
             },
    room126: {
                id: 1144, 
                guid: '0KLkXPBfvES9D1y7Ejijko', 
             }
            }

const initialQuery = `PREFIX inst: <http://linkedbuildingdata.net/ifc/resources20201208_005325/>
  PREFIX bot: <https://w3id.org/bot#>
  select * where { inst:space_${value} ?p ?guid}`;

 async function executePropertyNameQuery () {
  try {
  let token
  if (context.user && context.user.token) {
  token = context.user.token
   } 

  const resultList = await queryMultiple(context.currentProject.id, query, context.currentProject.activeGraphs, token) 
  const selection = [] 
  const predicat = [] 
  resultList.results.bindings.forEach((item) => {
    return(
      selection.push({guid: item.guid.value}),
      predicat.push({p: item.p.value}))
  })
  setContext({...context, selection, predicat}) 
   } catch (error) {
  console.log('error', error)
   }
  }   


   function onSelect(guids) {
     console.log("selected elements", guids);
     executePropertyNameQuery()
    setContext({ ...context, selection: guids});
   } 

Aucun commentaire:

Enregistrer un commentaire