我有以下数组:
this.userService.searchListingById(this.categoryId)
.subscribe(res => {
this.categoryRawdata = res.json();
});
我想将此对象数组转换为包含两个对象的数组,其中两个对象包含男性和女性的总计数据(计数,weightedCount和matchingSegment)。是否有使用Lodash和/或ES6实现此目的的简便方法?
更新:抱歉,我提供了错误的数据。
答案 0 :(得分:2)
~/Documents/projects/tests/Laravel1/HTML/storage/logs
答案 1 :(得分:1)
您可以按期望值分组并获得total
的总和。
var data = array = [{ text: "Male 18-24 (12886)", value: "test_1", total: 12886 }, { text: "Male 25-39 (27913)", value: "test_2", total: 27913 }, { text: "Male 40-54 (29793)", value: "test__3", total: 29793 }, { text: "Male 55+ (54888)", value: "test__4", total: 54888 }, { text: "Female 18-24 (19354)", value: "test_5", total: 19354 }, { text: "Female 25-39 (43972)", value: "test_6", total: 43972 }, { text: "Female 40-54 (39327)", value: "test_7", total: 39327 }, { text: "Female 55+ (52263)", value: "test_8", total: 52263 }],
result = _(data)
.groupBy(o => o.text.match(/^\S+/)[0])
.map((array, group) => ({ group, sum: _.sumBy(array, 'total') }))
.value();
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.min.js"></script>
答案 2 :(得分:1)
const { Sum, mreduceMap, compose, propOr, ifElse, constant } = crocks
const data = [
{text: "Male 18-24 (12886)", value: "test_1", total: 12886},
{text: "Male 25-39 (27913)", value: "test_2", total: 27913},
{text: "Male 40-54 (29793)", value: "test__3", total: 29793},
{text: "Male 55+ (54888)", value: "test__4", total: 54888},
{text: "Female 18-24 (19354)", value: "test_5", total: 19354},
{text: "Female 25-39 (43972)", value: "test_6", total: 43972},
{text: "Female 40-54 (39327)", value: "test_7", total: 39327},
{text: "Female 55+ (52263)", value: "test_8", total: 52263}
]
const startsWith = x => str =>
typeof str === 'string' &&
str.startsWith(x)
const isSex = sex =>
compose(startsWith(sex), propOr('', 'text'))
const totalFor = sex =>
ifElse(isSex(sex), propOr(0, 'total'), constant(0))
const sumSex = sex =>
mreduceMap(Sum, totalFor(sex))
const sumMales = sumSex('Male')
const sumFemales = sumSex('Female')
const males = sumMales(data)
const females = sumFemales(data)
console.log({males, females})
<script src="https://unpkg.com/crocks@0.10.1/dist/crocks.min.js"></script>
借助crocks
库,您可以使用功能强大的方法来解决问题。
答案 3 :(得分:1)
您可以使用reduce方法
var list = [
{genderAge: "Male 25-39", count: 13029, weightedCount: 18475.6824262314, matchingSegment: 2},
{genderAge: "Female 55+", count: 35639, weightedCount: 32294.5926147014, matchingSegment: 8},
{genderAge: "Female 25-39", count: 23285, weightedCount: 20645.0599977815, matchingSegment: 6},
{genderAge: "Female 18-24", count: 7745, weightedCount: 8497.30029399032, matchingSegment: 5},
{genderAge: "Male 55+", count: 38018, weightedCount: 28589.2793936886, matchingSegment: 4},
{genderAge: "Male 18-24", count: 4038, weightedCount: 8122.65161312996, matchingSegment: 1},
{genderAge: "Female 40-54", count: 23051, weightedCount: 22834.3167597392, matchingSegment: 7},
{genderAge: "Male 40-54", count: 17278, weightedCount: 19681.8852663563, matchingSegment: 3}
]
list.reduce(function(a, b) {
if(/^Male/.test(b['genderAge'])) {
a['male']['pop'] += b['count'];
a['male']['matchingSegment'] += b['matchingSegment'];
a['male']['weightedCount'] += b['weightedCount'];
} else if(/^Female/.test(b['genderAge'])){
a['female']['pop'] += b['count'];
a['female']['matchingSegment'] += b['matchingSegment'];
a['female']['weightedCount'] += b['weightedCount'];
}
return a;
}, {'male':{'pop':0, 'matchingSegment':0, 'weightedCount':0}, 'female':{'pop':0, 'matchingSegment':0, 'weightedCount':0}});
答案 4 :(得分:0)
ES6
.reduce((acc,e)=>{
if (/^Male/.test(e.text)){
acc[0].total+=e.total
}
if (/^Female/.test(e.text)){
acc[1].total+=e.total
}
return acc
},[{total:0},{total:0}])
答案 5 :(得分:0)
您可以简单地用两个extern "C" {
JNIEXPORT jobjectArray JNICALL Java_com_testjniproject_MainActivity_AnalyseImageDisplay(
JNIEnv *env,
jobject instance,
jlong sourceImageArray) {
jobjectArray result;
result = (jobjectArray)env->NewObjectArray(3,env->FindClass("java/lang/String"),env->NewStringUTF(""));
for(int i=0; i<3; i++) {
env->SetObjectArrayElement(result,i,env->NewStringUTF(finalResult[i].c_str()));
}
return result;
}
}
初始化array
,第一个用于objects
,第二个用于Male
。
Female
然后在您的第一个let result = [{text: "Male", total:0}, {text: "Female", total:0}];
上通过forEach
循环用相关数据填充它。
array
演示:
array.forEach(function(a){
if(a.genderAge.indexOf("Male")>-1){
result[0].total += a.count;
}else if(a.genderAge.indexOf("Female")>-1){
result[1].total += a.count;
}
});
答案 6 :(得分:0)
这是另一种简单的lodash方法,它使用_.groupBy并且仅执行一个循环:
var data = [ {genderAge: "Male 25-39", count: 13029, weightedCount: 18475.6824262314, matchingSegment: 2}, {genderAge: "Female 55+", count: 35639, weightedCount: 32294.5926147014, matchingSegment: 8}, {genderAge: "Female 25-39", count: 23285, weightedCount: 20645.0599977815, matchingSegment: 6}, {genderAge: "Female 18-24", count: 7745, weightedCount: 8497.30029399032, matchingSegment: 5}, {genderAge: "Male 55+", count: 38018, weightedCount: 28589.2793936886, matchingSegment: 4}, {genderAge: "Male 18-24", count: 4038, weightedCount: 8122.65161312996, matchingSegment: 1}, {genderAge: "Female 40-54", count: 23051, weightedCount: 22834.3167597392, matchingSegment: 7}, {genderAge: "Male 40-54", count: 17278, weightedCount: 19681.8852663563, matchingSegment: 3} ]
const getGenderTotals = data => {
let Males = 0, Females = 0
_.groupBy(data, (r => {/^Male/.test(r.genderAge) ? Males += r.count : Females += r.count}))
return { Males, Females }
}
console.log(getGenderTotals(data))
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>
由于我们可以访问该值,并且我们处于_.groupBy
上下文中,因此您还可以轻松地在groupBy iteratee函数内部返回Male
或Female
来获取实际的组等。 / p>