JSFiddle - React, Tailwind, and code Playground
by CommandLineDesign
JavaScript
var key = {
0: {
name: 'Socket',
len: 1
},
1: {
name: 'Length',
len: 2
},
2: {
name: 'Color',
len: 1
},
3: {
name: 'Finish',
len: 1
}
}
var pid = '700TDSOCOPV08OZ';
var parentId = '700TDSOCOP';
var productIdToSelections = function(parentId, productId, key){
var attributes = productId.split(parentId)[1];
var result = {}
var head = 0;
var tail = 0;
for(var i in key){
tail = key[i].len;
result[key[i].name] = attributes.substr(head, tail);
head = tail;
}
return result;
}
console.log(productIdToSelections(parentId, pid, key));