这个Javascript对象语法是什么意思?它具有一个键(一个字符串)和一个值(对象)

我正在关注有关在YouTube上将数据导入到React中现有待办事项列表组件中的教程。

If you look at the code below, at the const data object, there are two keys namely lists and listIds. There are two parts which I don't understand.

  1. Why is the key "list-1" a string while the value {id: "list-1",title: "Todo",cards,} is a normal object? I could not figure out this syntax. If it's JSON format, both key-value should be a in quotation marks.

  2. Is the listIds: ["list-1"] just a normal key-value pair which has an array as its value? If so, why does it has the same name as the one from the initial lists keys? Is this a Destructuring method from ES6?. I just cannot understand the syntax.

const cards = [
              {
                id: "card-1",
                title: "Learning how to cook",
              },
              {
                id: "card-2",
                title: "Making sandwich",
              },
              {
                id: "card-3",
                title: "Taking the trash out",
              },
            ];
        
        const data = {
          lists: {
            "list-1": {
              id: "list-1",
              title: "Todo",
              cards,
            },
          },
          listIds: ["list-1"],
        };
        
        export default data;
评论
墨小沫
墨小沫

因为“ list-1”包含减号,这将是标识符名称的错误。这就像试图从“列表”中减去1并将该表达式用作键一样。

listIds:[“ list-1”]是一个普通的JS键值表达式,其左侧的键为:,右侧为一个字符串值的数组。

点赞
评论