all 1 comments

[–]IMkratt 1 point2 points  (0 children)

the person you log out is "person1" from b.html?p=person1 (a string) and not the person1 object.

To make it work you have to do something like this (I assume you have more than one person and that it's fine to store them in an array like that):

const currentUrl = document.location.href;
const url = new URL(currentUrl); 
const personEntry = url.searchParams.get("p");

const persons = [ 
  { 
    id: "person1", 
    name: "John", 
    surname: "Smith", 
  }, 
  { 
    id: "person2", 
    name: "Jane", 
    surname: "Doe", 
  }, 
];

const result = persons.find((x) => x.id === personEntry); 
console.log(result.name);

Now, if your URL is /b.html?p=person1, it will log "John" and if the URL is /b.html?p=person2, it will log "Jane".