在 javascript 中无法读取 google 客户搜索结果对象的结果

trouble reading result of google customer search results object in javascript

有人知道阅读 Google 自定义搜索结果对象时要使用的语法吗?

这是来自 https://developers.google.com/custom-search/docs/element 的结果对象的 JSDoc 表示法:

{
  content: string,
  contentNoFormatting: string,
  contextUrl: string, // For image search results only
  fileFormat: string,
  image: { // For image search reseults only
    height: number,
    url: string,
    width: number,
  },
  perResultLabels: !Array<{
    anchor: string,
    label: string,
    labelWithOp: string,
  }>,
  richSnippet: !Array<!Object>, // For web search results only
  thumbnailImage: {
    height: number,
    url: string,
    width: number,
  },
  title: string,
  titleNoFormatting: string,
  url: string,
  visibleUrl: string,
} 

我需要获取 "url" (thumbnailimage.url) 但不确定用于引用它的语法。

这里是我需要使用它的地方。见下方评论

here is where I need to use it.  See comment below

const makeResultParts = (result) => {
const anchor = document.createElement('a');
anchor.href = result['thumbnailImage'] //  <<---this needs to be the thumbnailImage.url but that syntax does not work
anchor.target = '_blank';
anchor.classList.add('gs_title');
anchor.appendChild(document.createTextNode(result['visibleUrl']));
const span = document.createElement('span');
span.innerHTML = ' ' + result['title'];
return [anchor, span];
};

假设 result['thumbnailImage'] 总是存在(根据它应该用于图像搜索的文档),这应该适合你:

const makeResultParts = (result) => {
  const anchor = document.createElement('a');
  anchor.href = result['thumbnailImage']['url'] //  <<--- Access the url attribute of the thumbnailImage entry
  anchor.target = '_blank';
  anchor.classList.add('gs_title');
  anchor.appendChild(document.createTextNode(result['visibleUrl']));
  const span = document.createElement('span');
  span.innerHTML = ' ' + result['title'];
  return [anchor, span];
};