search

Home  >  Q&A  >  body text

javascript - To override the weight of a field in items with different IDs, is there a more elegant and easy-to-maintain way besides directly using the if else statement?

There is a batch of products, all of which contain a config field. Now there are products with different IDs in a list, and the required config field needs to be taken out according to certain weight rules:
For example, the id is 1, The weight of 3 is higher than 2 and 4, and the weight of id 2 and 4 is higher than 5 and 7
So how can we design the code other than simply using if-else to operate? So that it is easier to maintain

巴扎黑巴扎黑2821 days ago750

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-07-05 10:53:39

    switch? It’s better to encapsulate it into a function
    if else is also ok, it’s not complicated

    reply
    0
  • Cancelreply