玩过淘宝数据接口的估计知道sku是什么,而我是首次听,虽然我上家公司分析过淘宝商家数据。
什么是sku,我过几天再补充,我先发我理解的sku数据格式实现代码,大概是这么个意思吧。
<script>
var data = [
{
"d": '1232323',
"v": '2331:23213;1323:123243;1:2',
"b": '颜色:黑色;尺码:30;大小:sm'
},
{
"d": '1232323',
"v": '2331:2323;1323:12243',
"b": '颜色:白色;尺码:38'
},
{
"d": '1232323',
"v": '2331:23;1323:12243',
"b": '颜色:紫色;尺码:38'
}
]
var formated_data = [
{
"id": null,
"title": null,
"values": [],
"labels": []
}
];
data.forEach( function( obj ){
for( var key in obj ){
if ( key == "v" ) {
var vArr = obj[key].split( ';' );
var bArr = obj['b'].split( ';' );
vArr.forEach( function( item, index ){
var subVArr = item.split( ':' );
var subBArr = bArr[index].split( ':' );
subVArr.forEach( function( subVItem, subVIndex ){
if ( subVIndex == 0 ) {
addData( formated_data, subVItem, subBArr[1], subBArr[0], subVArr[1] )
}
} )
} )
};
}
} )
function addData( formated_data, id, value, title, label ){
var canPush = true;
formated_data.forEach(function( item, index ){
for( var formated_key in item ){
if ( formated_key == 'id' && item[formated_key] == id ) {
canPush = false;
if ( item.values != undefined && item.values.indexOf( value ) > -1 ) {
}else{
item.values.push( value );
}
if ( item.labels != undefined && item.labels.indexOf( label ) > -1 ) {
}else{
item.labels.push( label );
}
}
}
});
if ( canPush ) {
formated_data.push( {
"id": id,
"title": title,
"values": [value],
"labels": [label]
} )
}
}
formated_data.shift();
console.log( formated_data );
</script>
查看官方给出的答案,非常精简:
var indexOf = function(val, arr){
for(var i = 0; i < arr.length; i++){
if(arr[i] == val){
return i;
};
};
return -1;
};
var maps = {} //缓存键值
, skuArr = []; //转换后的SKU对象数组
arr.forEach(function(item){ //循环遍历原数组
var idRegexp = item.v.match(/([\d]+)/ig) //正则匹配ID
, nameRegexp = item.b.match(/([^\;\:]+)/ig); //正则匹配名称
idRegexp.forEach(function(id, index){ //遍历ID
if(index % 2 === 0){ //通过游标判断当前ID是属性还是属性值
if(maps[id] == null){ //如果为属性ID就创建一个SKU对象
maps[id] = skuArr.length; //将SKU对象所在下标记录下来, 方便后续判断SKU是否已存在
skuArr.push({ //添加一个SKU对象
'id': id
, 'title': nameRegexp[index]
, 'values': []
, 'labels': []
});
};
}else{ //属性值
var sku = skuArr[maps[idRegexp[index - 1]]]; //获取SKU对象
if(indexOf(id, sku.values) < 0){ //如果SKU属性值不存在
sku.values.push(id); //添加SKU属性值ID
sku.labels.push(nameRegexp[index]); //添加SKU属性值名称
};
};
});
});
console.log(JSON.stringify(skuArr, null, 4)); //console输出结果
测试数据在这里:
var arr = [{"d":"754630","v":"1627207:28341;20518:662626359","b":"颜色:黑色;尺码:28"},{"d":"754683","v":"1627207:28337;20518:103189693","b":"颜色:浅蓝色;尺码:38"},{"d":"754632","v":"1627207:28341;20518:444706729","b":"颜色:黑色;尺码:30"},{"d":"754633","v":"1627207:28341;20518:662626362","b":"颜色:黑色;尺码:31"},{"d":"754634","v":"1627207:28341;20518:73406764","b":"颜色:黑色;尺码:32"},{"d":"754635","v":"1627207:28341;20518:662626364","b":"颜色:黑色;尺码:33"},{"d":"754636","v":"1627207:28341;20518:147478970","b":"颜色:黑色;尺码:34"},{"d":"754637","v":"1627207:28341;20518:59280855","b":"颜色:黑色;尺码:36"},{"d":"754638","v":"1627207:28341;20518:103189693","b":"颜色:黑色;尺码:38"},{"d":"754639","v":"1627207:28331;20518:662626359","b":"颜色:卡其色;尺码:28"},{"d":"754640","v":"1627207:28331;20518:662626360","b":"颜色:卡其色;尺码:29"},{"d":"754641","v":"1627207:28331;20518:444706729","b":"颜色:卡其色;尺码:30"},{"d":"754642","v":"1627207:28331;20518:662626362","b":"颜色:卡其色;尺码:31"},{"d":"754643","v":"1627207:28331;20518:73406764","b":"颜色:卡其色;尺码:32"},{"d":"754644","v":"1627207:28331;20518:662626364","b":"颜色:卡其色;尺码:33"},{"d":"754645","v":"1627207:28331;20518:147478970","b":"颜色:卡其色;尺码:34"},{"d":"754646","v":"1627207:28331;20518:59280855","b":"颜色:卡其色;尺码:36"},{"d":"754647","v":"1627207:28331;20518:103189693","b":"颜色:卡其色;尺码:38"},{"d":"754648","v":"1627207:30155;20518:662626359","b":"颜色:杏色;尺码:28"},{"d":"754649","v":"1627207:30155;20518:662626360","b":"颜色:杏色;尺码:29"},{"d":"754650","v":"1627207:30155;20518:444706729","b":"颜色:杏色;尺码:30"},{"d":"754651","v":"1627207:30155;20518:662626362","b":"颜色:杏色;尺码:31"},{"d":"754652","v":"1627207:30155;20518:73406764","b":"颜色:杏色;尺码:32"},{"d":"754653","v":"1627207:30155;20518:662626364","b":"颜色:杏色;尺码:33"},{"d":"754654","v":"1627207:30155;20518:147478970","b":"颜色:杏色;尺码:34"},{"d":"754655","v":"1627207:30155;20518:59280855","b":"颜色:杏色;尺码:36"},{"d":"754656","v":"1627207:30155;20518:103189693","b":"颜色:杏色;尺码:38"},{"d":"754657","v":"1627207:3232484;20518:662626359","b":"颜色:天蓝色;尺码:28"},{"d":"754658","v":"1627207:3232484;20518:662626360","b":"颜色:天蓝色;尺码:29"},{"d":"754659","v":"1627207:3232484;20518:444706729","b":"颜色:天蓝色;尺码:30"},{"d":"754660","v":"1627207:3232484;20518:662626362","b":"颜色:天蓝色;尺码:31"},{"d":"754661","v":"1627207:3232484;20518:73406764","b":"颜色:天蓝色;尺码:32"},{"d":"754662","v":"1627207:3232484;20518:662626364","b":"颜色:天蓝色;尺码:33"},{"d":"754663","v":"1627207:3232484;20518:147478970","b":"颜色:天蓝色;尺码:34"},{"d":"754664","v":"1627207:3232484;20518:59280855","b":"颜色:天蓝色;尺码:36"},{"d":"754665","v":"1627207:3232484;20518:103189693","b":"颜色:天蓝色;尺码:38"},{"d":"754666","v":"1627207:3707775;20518:662626359","b":"颜色:宝蓝色;尺码:28"},{"d":"754667","v":"1627207:3707775;20518:662626360","b":"颜色:宝蓝色;尺码:29"},{"d":"754668","v":"1627207:3707775;20518:444706729","b":"颜色:宝蓝色;尺码:30"},{"d":"754669","v":"1627207:3707775;20518:662626362","b":"颜色:宝蓝色;尺码:31"},{"d":"754670","v":"1627207:3707775;20518:73406764","b":"颜色:宝蓝色;尺码:32"},{"d":"754671","v":"1627207:3707775;20518:662626364","b":"颜色:宝蓝色;尺码:33"},{"d":"754672","v":"1627207:3707775;20518:147478970","b":"颜色:宝蓝色;尺码:34"},{"d":"754673","v":"1627207:3707775;20518:59280855","b":"颜色:宝蓝色;尺码:36"},{"d":"754674","v":"1627207:3707775;20518:103189693","b":"颜色:宝蓝色;尺码:38"},{"d":"754675","v":"1627207:28337;20518:662626359","b":"颜色:浅蓝色;尺码:28"},{"d":"754676","v":"1627207:28337;20518:662626360","b":"颜色:浅蓝色;尺码:29"},{"d":"754677","v":"1627207:28337;20518:444706729","b":"颜色:浅蓝色;尺码:30"},{"d":"754678","v":"1627207:28337;20518:662626362","b":"颜色:浅蓝色;尺码:31"},{"d":"754679","v":"1627207:28337;20518:73406764","b":"颜色:浅蓝色;尺码:32"},{"d":"754680","v":"1627207:28337;20518:662626364","b":"颜色:浅蓝色;尺码:33"},{"d":"754681","v":"1627207:28337;20518:147478970","b":"颜色:浅蓝色;尺码:34"},{"d":"754682","v":"1627207:28337;20518:59280855","b":"颜色:浅蓝色;尺码:36"},{"d":"754631","v":"1627207:28341;20518:662626360","b":"颜色:黑色;尺码:29"}];
