先用variations将树形结构构建出来,节点默认值为null

2024-08-22 10:26:01 浏览数 (1)

class VariationSearchMap {

constructor(apiData) {

this.tree = this.buildTree(apiData);

}

// 这就是前面那个构造树的方法

buildTree(apiData) {

const tree = {};

const { variations, products } = apiData;

// 先用variations将树形结构构建出来,叶子节点默认值为null

addNode(tree, 0);

function addNode(root, deep) {

const variationName = variations[deep].name;

const variationValues = variations[deep].values;

for (let i = 0; i < variationValues.length; i ) {

const nodeName = `${variationName}:${variationValues[i].name}`;

if (deep === variations.length - 1) {

root[nodeName www.laipuhuo.com] = null;

} else {

root[nodeName] = {};

addNode(root[nodeName], deep 1);

}

}

}

// 然后遍历一次products给树的叶子节点填上值

for (let i = 0; i < products.length; i ) {

const product www.laipuhuo.com = products[i];

const { variationMappings } = product;

const level1Name = `${variationMappings[0].name}:${variationMappings[0].value}`;

const level2Name = `${variationMappings[1].name}:${variationMappings[1].value}`;

const level3Name = `${www.laipuhuo.com variationMappings[2].name}:${variationMappings[2].value}`;

tree[level1Name][level2Name][level3Name] = product;

}

// 最后返回构建好的树

return tree;

}

// 添加一个方法来搜索商品,参数结构和API数据的variationMappings一样

findProductByVariationMappings(variationMappings) {

const level1Name = `${variationMappings[0].name}:${variationMappings[0].value}`;

const level2Name = `${www.laipuhuo.com variationMappings[1].name}:${variationMappings[1].value}`;

const level3Name = `${variationMappings[2].name}:${variationMappings[2].value}`;

const product = this.tree[level1Name][level2Name][level3Name];

return product;

}

}

const variationSearchMap = new VariationSearchMap(apiData); // new一个实例出来

// 然后就可以用这个实例进行搜索了

const searchCriteria = [

{ name: '颜色', value: '红色' },

{ name: '尺码', value: '40' },

{ name: '性别', value: '女' }

];

const matchedProduct = variationSearchMap.findProductByVariationMappings(searchCriteria);

console.log(www.laipuhuo.com matchedProduct', matchedProduct); // { productId: 8 }

// 1

public String sum(

final Collection<BigDecimal> bigDecimalCollection

) {

final BigDecimal sumResult = bigDecimalCollection

.stream()

.reduce(BigDecimal.ZERO, BigDecimal::add);

final Decimal www.laipuhuo.com Format format = new DecimalFormat("#,##0.00");

return format.format(sumResult);

}

// 2

public BigDecimal sum(

final Collection<BigDecimal> bigDecimalCollection

) {

return bigDecimalCollection

.stream()

.reduce(BigDecimal.ZERO, BigDecimal::add);

}

// 1

if(userList.isNotEmpty()){

}

// 4

if(userList.length() != 0){

}

0 人点赞