首页 > 解决方案 > 从 javascript 中的另一个对象访问嵌套属性

问题描述

我对下面的代码如何访问嵌套属性感到困惑。该addLayer函数接受一个对象,该对象具有textfield来自上述addSource方法的嵌套属性properties.storeId。我对如何textfield写成{storeId}. 该对象在任何地方都没有被破坏,因此它怎么能这样写?

const loadmap = () => {
  map.on("load", () => {

    map.addSource("point", {
      type: "geojson",
      data: {
        type: "FeatureCollection",
        features: [
          {
            type: "Feature",
            geometry: {
              type: "Point",
              coordinates: [-71.157895, 42.707741],
            },
            properties: {
              storeId: "0001",
              icon: "store"
            },
          },
        ],
      },
    });

    map.addLayer({
      id: "points",
      type: "symbol",
      source: "point", // reference the data source
      layout: {
        "icon-image": "airfield-11", // reference the image
        "text-field": "{storeId}",
      },
    });
  });
};

标签: javascripttypescriptjavascript-objectsobject-destructuring

解决方案


双引号之间的 {storeId} 只是一个字符串,

否则它是

let object={storeId:storeId}

另一个例子:

let name="erfan"
let obj = {name} /// equivalent to {name:name}

推荐阅读