function countFieldPersent(arr, field) {
var data = arr.reduce((r, item) => {
var value = String(item[field]);
if(typeof r[value] === 'undefined') {
r[value] = 0;
}
return ++r[value], r;
}, {});
return Object.getOwnPropertyNames(data).map(label => ({
label,
value : (data[label] * 100) / arr.length
}));
}
var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var result = countFieldPersent(arr, 'Gender');
Правда в моем варианте значения будут сравниваться как строки, так как я их перевожу в ключи объекта data var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var count = 0;
for (i=0;i<arr.length;i++){
if (arr[i].Gender == 'male'){
count++;
}
}
var percentMale = Math.round(count / arr.length * 100);
var percentFemale = 100 - percentMale;
var arr2 = [{"label":"Male", "value":percentMale},
{"label":"Female", "value":percentFemale}];
var arr = [{ FirstName: 'Pavel', LastName: 'Pavlol', Phone: '1234567', Gender:'male' },
{ FirstName: 'Ivan', LastName: 'Ivanov', Phone: '1234567', Gender:'female' }];
var males = arr.filter((item)=>(item.Gender == 'male' ));
var females = arr.filter((item)=>(item.Gender == 'female'));
var result = [{
"label" : "male",
"value" : (males.length*100)/arr.length
},
{
"label" : "female",
"value" : (females.length*100)/arr.length
}]
function mergeCells(table) {
var head = table.rows[0],
body = table.rows[1],
resultHead = "",
resultBody = "",
matches = {};
for (var i = 0; i < body.cells.length; i++) {
var headCellText = head.cells[i].textContent,
bodyCellText = body.cells[i].textContent,
match = matches[bodyCellText] || (matches[bodyCellText] = []);
match.push(headCellText);
}
for (var key in matches) {
var match = matches[key],
from = match[0],
to = match[match.length - 1];
if (match.length > 1) {
resultHead += "<td>" + from + "-" + to + "</td>";
} else {
resultHead += "<td>" + from + "</td>";
}
resultBody += "<td>" + key + "</td>";
}
head.innerHTML = resultHead;
body.innerHTML = resultBody;
}
<table border>
<thead>
<td>ПН</td>
<td>ВТ</td>
<td>СР</td>
<td>ЧТ</td>
<td>ПТ</td>
<td>СБ</td>
<td>ВС</td>
</thead>
<tr id="state">
<td>Выходной</td>
<td>Выходной</td>
<td>Выходной</td>
<td>Выходной</td>
<td>15-21</td>
<td>15-21</td>
<td>16-21</td>
</tr>
</table>
var
$Table = $("<table />"),
$Thead = $("<thead />").appendTo($Table),
$Row = $("<tr />").appendTo($Table),
$_tds = $("#state td"), $_heads = $("thead td"),
start, i = 0;
for(; i < $_tds.length; i++){
start = i;
while(
$_tds.get(i++) && $_tds.eq(i).text() == $_tds.eq(i - 1).text()
){}
$Row.append(
"<td>" + $_tds.eq(--i).text() + "</td>"
);
$Thead.append(
"<td>" + (i == start ? "" : $_heads.eq(start).text() + "-") + $_heads.eq(i).text() + "</td>"
);
}
$("table").replaceWith($Table);
id | name
id | name
guide_id | category_id
public function categories()
{
return $this->belongsToMany('App\Models\Category');
}
public function guides()
{
return $this->belongsToMany('App\Models\Guide');
}
$ids = [1, 2, 3];
$guides = App\Models\Guide::whereHas('categories', function ($q) use ($ids)
{
return $q->whereIn('id', $ids);
})->get();
$category = Category::find(1);
$count = $category->guides()->count();
// Модель Cloth
public function styles()
{
return $this->belongsToMany(...);
}
$result = Cloth::whereHas('styles', function ($q)
{
return $q->whereIn('id', [1, 2]);
})->get();