- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
给出按首字母排序的城市对象列表。我需要将它们分成数组内的 4 个数组,以便将它们列在 col-md-3 中的 4 行中。
我现在有这个代码:
<div class="container">
<div class="row">
<div class="col-md-3 col-sm-3" v-for="data, index in sortedCities">
<ul>
<li>{{ index }}</li>
<li v-for="city in data">{{ city.name }}</li>
</ul>
</div>
</div>
</div>
并使用计算属性进行排序:
computed: {
sortedCities() {
var newCities = {}
for (var i = 0; i < this.cities.length; i++) {
var c = this.cities[i].name[0].toUpperCase()
if (newCities[c] && newCities[c].length >= 0)
newCities[c].push(this.cities[i]);
else {
newCities[c] = [];
newCities[c].push(this.cities[i]);
}
}
return newCities
}
}
请帮助制作 4 行,每行 24 行,并进行排序,如果排序的项目以某个字母结尾,则新行必须以该字母继续。抱歉我的英语)
这是一个样板:https://jsfiddle.net/3p3goy6u/
第一组元素中的 2 个组以字母 B 结尾并在第二组中继续的示例:
[
"firstGroup": [
"A": [{
id: 1,
slug: "abakan",
name: "Абакан"
}, {
id: 4,
slug: "almetevsk",
name: "Альметьевск"
}, ],
"B": [{
id: 11,
slug: "barnaul",
name: "Барнаул"
}, {
id: 13,
slug: "belgorod",
name: "Белгород"
}, ]
],
"secondGroup": [
"B": [{
id: 19,
slug: "bryansk",
name: "Брянск"
}, ]
]
]
最佳答案
我仍然不明白你想在这里实现什么,但根据你的预期输出,它可以通过做这样的事情来实现。
var cities = [{"id": 1, "slug": "abakan", "name": "Абакан"}, {"id": 4, "slug": "almetevsk", "name": "Альметьевск"}, {"id": 5, "slug": "armavir", "name": "Армавир"}, {"id": 7, "slug": "arhangelsk", "name": "Архангельск"}, {"id": 10, "slug": "astrahan", "name": "Астрахань"}, {"id": 11, "slug": "barnaul", "name": "Барнаул"}, {"id": 13, "slug": "belgorod", "name": "Белгород"}, {"id": 14, "slug": "biysk", "name": "Бийск"}, {"id": 16, "slug": "blagoveshchensk", "name": "Благовещенск"}, {"id": 17, "slug": "bratsk", "name": "Братск"}, {"id": 19, "slug": "bryansk", "name": "Брянск"}, {"id": 21, "slug": "velikiy-novgorod", "name": "Великий Новгород"}, {"id": 22, "slug": "vladivostok", "name": "Владивосток"}, {"id": 24, "slug": "vladimir", "name": "Владимир"}, {"id": 26, "slug": "volgograd", "name": "Волгоград"}, {"id": 29, "slug": "vologda", "name": "Вологда"}, {"id": 30, "slug": "voronezh", "name": "Воронеж"}, {"id": 32, "slug": "gorno-altaysk", "name": "Горно-Алтайск"}, {"id": 33, "slug": "ekaterinburg", "name": "Екатеринбург"}, {"id": 41, "slug": "ivanovo", "name": "Иваново"}, {"id": 43, "slug": "izhevsk", "name": "Ижевск"}, {"id": 44, "slug": "irkutsk", "name": "Иркутск"}, {"id": 47, "slug": "yoshkar-ola", "name": "Йошкар-Ола"}, {"id": 48, "slug": "kavkazskie-mineralnye-vody", "name": "Кавказские Минеральные Воды"}, {"id": 55, "slug": "kazan", "name": "Казань"}, {"id": 59, "slug": "kaliningrad", "name": "Калининград"}, {"id": 68, "slug": "kaluga", "name": "Калуга"}, {"id": 69, "slug": "kamensk-uralskiy", "name": "Каменск-Уральский"}, {"id": 70, "slug": "kemerovo", "name": "Кемерово"}, {"id": 71, "slug": "kirov", "name": "Киров"}, {"id": 72, "slug": "komsomolsk-na-amure", "name": "Комсомольск-на-Амуре"}, {"id": 74, "slug": "kostroma", "name": "Кострома"}, {"id": 75, "slug": "krasnodar", "name": "Краснодар"}, {"id": 76, "slug": "krasnoyarsk", "name": "Красноярск"}, {"id": 80, "slug": "kurgan", "name": "Курган"}, {"id": 81, "slug": "kursk", "name": "Курск"}, {"id": 82, "slug": "leninsk-kuzneckiy", "name": "Ленинск-Кузнецкий"}, {"id": 87, "slug": "lipeck", "name": "Липецк"}, {"id": 88, "slug": "magnitogorsk", "name": "Магнитогорск"}, {"id": 90, "slug": "mahachkala", "name": "Махачкала"}, {"id": 92, "slug": "miass-i-zlatoust", "name": "Миасс и Златоуст"}, {"id": 94, "slug": "moskva", "name": "Москва"}, {"id": 147, "slug": "murmansk", "name": "Мурманск"}, {"id": 149, "slug": "naberezhnye-chelny", "name": "Набережные Челны"}, {"id": 152, "slug": "nahodka", "name": "Находка"}, {"id": 153, "slug": "nizhnevartovsk", "name": "Нижневартовск"}, {"id": 155, "slug": "nizhniy-novgorod", "name": "Нижний Новгород"}, {"id": 160, "slug": "nizhniy-tagil", "name": "Нижний Тагил"}, {"id": 161, "slug": "novokuzneck", "name": "Новокузнецк"}, {"id": 166, "slug": "novorossiysk", "name": "Новороссийск"}, {"id": 171, "slug": "novosibirsk", "name": "Новосибирск"}, {"id": 177, "slug": "norilsk", "name": "Норильск"}, {"id": 179, "slug": "noyabrsk", "name": "Ноябрьск"}, {"id": 181, "slug": "omsk", "name": "Омск"}, {"id": 182, "slug": "orenburg", "name": "Оренбург"}, {"id": 183, "slug": "orel", "name": "Орёл"}, {"id": 184, "slug": "penza", "name": "Пенза"}, {"id": 186, "slug": "perm", "name": "Пермь"}, {"id": 188, "slug": "petrozavodsk", "name": "Петрозаводск"}, {"id": 189, "slug": "petropavlovsk-kamchatskiy", "name": "Петропавловск-Камчатский"}, {"id": 192, "slug": "pskov", "name": "Псков"}, {"id": 193, "slug": "rostov-na-donu", "name": "Ростов-на-Дону"}, {"id": 197, "slug": "ryazan", "name": "Рязань"}, {"id": 199, "slug": "samara", "name": "Самара"}, {"id": 201, "slug": "sankt-peterburg", "name": "Санкт-Петербург"}, {"id": 210, "slug": "saransk", "name": "Саранск"}, {"id": 212, "slug": "saratov", "name": "Саратов"}, {"id": 214, "slug": "smolensk", "name": "Смоленск"}, {"id": 215, "slug": "sochi", "name": "Сочи"}, {"id": 217, "slug": "stavropol", "name": "Ставрополь"}, {"id": 219, "slug": "staryy-oskol", "name": "Старый Оскол"}, {"id": 220, "slug": "sterlitamak", "name": "Стерлитамак"}, {"id": 223, "slug": "surgut", "name": "Сургут"}, {"id": 225, "slug": "syktyvkar", "name": "Сыктывкар"}, {"id": 226, "slug": "tambov", "name": "Тамбов"}, {"id": 228, "slug": "tver", "name": "Тверь"}, {"id": 229, "slug": "tobolsk", "name": "Тобольск"}, {"id": 230, "slug": "tolyatti", "name": "Тольятти"}, {"id": 232, "slug": "tomsk", "name": "Томск"}, {"id": 233, "slug": "tula", "name": "Тула"}, {"id": 242, "slug": "tyumen", "name": "Тюмень"}, {"id": 245, "slug": "ulan-ude", "name": "Улан-Удэ"}, {"id": 246, "slug": "ulyanovsk", "name": "Ульяновск"}, {"id": 248, "slug": "ussuriysk", "name": "Уссурийск"}, {"id": 249, "slug": "ufa", "name": "Уфа"}, {"id": 250, "slug": "habarovsk", "name": "Хабаровск"}, {"id": 251, "slug": "cheboksary", "name": "Чебоксары"}, {"id": 253, "slug": "chelyabinsk", "name": "Челябинск"}, {"id": 255, "slug": "chita", "name": "Чита"}, {"id": 256, "slug": "yuzhno-sahalinsk", "name": "Южно-Сахалинск"}, {"id": 260, "slug": "yakutsk", "name": "Якутск"}, {"id": 261, "slug": "yaroslavl", "name": "Ярославль"}, {"id": 262, "slug": "pyatigorsk", "name": "Пятигорск"}];
var result = {},
row = 1,
counter = 0;
for (city of cities) {
var initial = city.name[0].toUpperCase(),
groupName = 'Group_' + row;
if (!result[groupName]) {
result[groupName] = {};
}
if (!result[groupName][initial]) {
result[groupName][initial] = [];
counter++;
}
result[groupName][initial].push(city);
counter++;
if (counter >= 23) {
row++;
counter = 0;
}
}
console.log(result);
这就是您的 fiddle 中的样子.
关于javascript - 将已排序的对象除以 4 以将它们列在 col-md-3 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43754417/
你有没有注意到 Excel Vba 代码中除法 2/60 的问题?我正在使用 Excel 2013。请测试以下代码: Sub test1() Dim A As Integer Dim B As Int
我正在运行长时间的模拟。我将结果记录到 vector 中以计算有关数据的统计信息。我意识到,理论上,这些样本可能是除以零的结果。这只是理论上的,我很确定事实并非如此。为了避免修改代码后重新运行模拟,我
我有一些可以包含字母、数字和“#”符号的字符串。 我想删除除以“#”开头的单词以外的数字 下面是一个例子: "table9 dolv5e #10n #dec10 #nov8e 23 hello" 预期
我有以下代码,问题是我尝试将 Double 除以 Int factorial :: Int -> Int factorial 0 = 1 factorial e = e * (factorial e-
我有以下查询,它试图计算出某种产品占产品总数的百分比。 IE:[产品数量]/[产品总数] = 百分比 ;WITH totalCount AS( SELECT CAST(COUN
我正在解析字符串,然后需要将其转换为数字。但如果它包含除以 0 的例子 String str1 = "1+2+3-5/0+4+6" String str2 = "1+2+3-4/0.000 +4+6"
这个问题已经有答案了: Right Shift to Perform Divide by 2 On -1 (6 个回答) 已关闭 9 年前。 在阅读 Collections.reverse 的 Jav
我是 C++ 新手。我听说除以0会导致运行时错误,但是当我尝试时,它抛给我一个编译器错误C2124并且没有创建目标文件,所以编译器会自动运行代码以查看它是否可以在创建之前执行目标文件? (顺便说一句,
我试图在 codefights.com 上解决这个问题,方法是找出最大的质因数并将该数字除以质因数的幂。 int highestPower(int N, int A) { int B =A, j=0,
我对在不同计算机上运行程序有疑问。 我用 C++\windows 7 64 位\Visual Studio 编写了一个程序,该程序在我的计算机上完美运行。 当我尝试在另一台计算机(Windows 7
这个问题已经有答案了: Division in C++ not working as expected (6 个回答) 已关闭 9 年前。 Helo,我是编程新手,遇到了一个问题,我有一个整数,例如
我正在做某事,但遇到了一个我不明白的问题。 double d = 95.24 / (double)100; Console.Write(d); //Break point here 控制台输出是 0.
我需要仅使用按位运算符(例如 ! & ^ ~ 和移位)来计算数字 (a/(2**b)。我得到了以下提示,但我是 C 新手,我不知道什么是代码含义: int bias = x>0 ? 0 : ((1>b
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: Java Integer division: How do you produce a double? 当我
嗨,我正在尝试将 numpy 数组分成两部分,并在一个循环中进行此操作,例如: main_array.shape = (50, 400, 400, 3) for i = 0: sub_array_1
这是片段: String myTime = someTime / 1e9d + ","; someTime 是使用 System.nanoTime() 派生的。 1e9d 在这里做什么? 最佳答案 1
Random random = new Random(); int randomx = random.Next(0, 240); 这是我获取随机数的方式,从 0 到 240,如何才能只获取除以 5 的
我一直在处理一个应用程序,因为图像表现得很奇怪,所以我决定打开 Wall 标志,看看是否有任何奇怪的事情发生。它揭示了一个新的警告: character.cpp(364): warning C4723
我是 Android Studio 的新手,我正在开发一个计算器应用程序。现在,当我除以零时,自然会收到错误消息“Infinity”,但如果我想将其更改为“不能除以零”,我该怎么做呢? impor
如何显示每 5 日创建一个新行的表中的数据 ? 例子 data: [1,2,3,4,5,6]; 成分: {{item}} 预期的: | 1 | 2 | 3 | 4 | 5 | | 6 |
我是一名优秀的程序员,十分优秀!