我正在尝试创建一个函数,只需让我输出CSS中简单二维地图的键名和键值:
$people: (
"Hellen": (
age: "34",
sex: "female"
),
"Patrick": (
age: "23",
sex: "male"
),
"George": (
age: "10",
sex: "male"
),
"Vicky": (
age: "19",
sex: "female"
)
);
我正在创建一个简单的函数来检索这些信息:
@each $person-name, $person-details in $people {
$age: map-get($person-details, 'age');
$sex: map-get($person-details, 'sex');
.#{$person-name} {
height: 100 px;
width: 100 px;
background: #FF3C00;
margin: 0 auto;
&:before {
content:$person-name " " + $age " ";
}
&:after {
content: $sex;
}
}
}
HTML:
<div class="Hellen"></div>
但我找不到任何将密钥和值都作为单独对象的信息。我只能通过以下内容读取值,而不是键:
$age: map-get($person-details, 'age');
结果:
Hellen 34 female
代替: 海伦年龄:34性别:女性
如何获得带或不带值的关键标签?
答案 0 :(得分:7)
使用map-keys()
获取列表中的所有密钥。
样品:
@each $person-name, $person-details in $people {
$age: map-get($person-details, 'age');
$sex: map-get($person-details, 'sex');
$keys: map-keys($person-details);
.#{$person-name} {
height: 100 px;
width: 100 px;
background: #FF3C00;
margin: 0 auto;
&:before {
content:"#{$person-name} #{nth($keys, 1)} : #{$age} ";
}
&:after {
content: "#{nth($keys, 2)}: #{$sex}";
}
}
}