查找最大匹配数

时间:2019-02-08 11:38:47

标签: javascript reactjs ecmascript-6

我必须从每个json对象中找到最大匹配的前缀,并将其与其他运算符进行比较,并找出价格最低的运算符

2 个答案:

答案 0 :(得分:1)

此脚本有效。首先找出所有运算符的最大前缀。然后找出最大的成本。

const operators = [{
    operatorA: [{
      prefix: 1,
      cost: 0.9
    }, {
      prefix: 268,
      cost: 5.1
    }, ]
  },
  {
    operatorB: [{
      prefix: 1,
      cost: 0.15
    }, {
      prefix: 268,
      cost: 0.9
    }]
  }
]



const maxPrefixArray = operators.map(operator => {

  return _.maxBy(operator[Object.keys(operator)[0]], "prefix")
})

const commonPrefix = maxPrefixArray.filter((operator, index, self) =>
  index === self.findIndex((t) => (
    t.prefix === operator.prefix
  ))
)



const minimalCost = _.minBy(commonPrefix, "cost")

console.log(minimalCost)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.js"></script>

答案 1 :(得分:1)

我为您提供了以下解决方案:

此代码的摘要:

  • 对于每个操作员,请执行以下操作:

    • 按前缀对反向排序
    • 找到第一个匹配项(数字以前缀开头)
    • 如果匹配,则在结果数组中收集匹配
  • 按成本对结果数组进行排序

  • 返回结果列表的第一项

console.clear();

operators = {
    "Operator A":[
        {"prefix" : 46732,"cost" : 1.1},
        {"prefix" : 46, "cost" : 0.17}, 
        {"prefix" : 4620, "cost" : 0.0}, 
        {"prefix" : 1, "cost" : 0.9}, 
        {"prefix" : 268, "cost" : 5.1}, 
        {"prefix" : 4631, "cost" : 0.15}, 
        {"prefix" : 4673, "cost" : 0.9}, 
        {"prefix" : 468, "cost" : 0.15}
    ],
    "Operator B":[
        {"prefix" : 46, "cost" : 0.2}, 
        {"prefix" : 44, "cost" : 0.5}, 
        {"prefix" : 48, "cost" : 1.2},
        {"prefix" : 467, "cost" : 1.0}, 
        {"prefix" : 1, "cost" : 0.92} 
    ],
    "Operator C":[
        {"prefix" : 46, "cost" : 0.1}, 
        {"prefix" : 44, "cost" : 0.25}, 
        {"prefix" : 48, "cost" : 2.4},
        {"prefix" : 467, "cost" : 2.0}, 
        {"prefix" : 1, "cost" : 0.46} 
    ],
};


let sortReverseByPrefix = (arr) => {
  arr.sort(function(a, b){
    return ""+a.prefix > ""+b.prefix
      ? -1
    : ""+a.prefix < ""+b.prefix
      ? 1
    : 0;
  });
}
let findCostForNumber = (arr, number) => {
  return arr.find(x => number.startsWith(x.prefix))
}
let sortByCost = (arr) => {
  arr.sort(function(a, b){
    return 0+a.cost < 0+b.cost
      ? -1
    : 0+a.cost > 0+b.cost
      ? 1
    : 0;
  });
}

let findCheapestOperator = (operators, number) => {
  let numberClean = (""+number).replace(/\D/g, '');
  let result = [];

  for (operator in operators) {
    sortReverseByPrefix(operators[operator])
    let match = findCostForNumber(operators[operator], numberClean);
    if (match) {
      result.push({operator, number,...match})
    }
  }
  sortByCost(result)
  if (! result ) return null
  return result[0];
}


s1 = findCheapestOperator(operators, '4420162012')
s2 = findCheapestOperator(operators, '1-4613520166')
s3 = findCheapestOperator(operators, '46-205297814')
s4 = findCheapestOperator(operators, '46-73-212345')
s5 = findCheapestOperator(operators, '48-737297242')



console.log(s1.number, " should be called with ", s1.operator, " for a cost of ", s1.cost, " with prefix", s1.prefix);
console.log(s2.number, " should be called with ", s2.operator, " for a cost of ", s2.cost, " with prefix", s2.prefix);
console.log(s3.number, " should be called with ", s3.operator, " for a cost of ", s3.cost, " with prefix", s3.prefix);
console.log(s4.number, " should be called with ", s4.operator, " for a cost of ", s4.cost, " with prefix", s4.prefix);
console.log(s5.number, " should be called with ", s5.operator, " for a cost of ", s5.cost, " with prefix", s5.prefix);