Javascript开关语句多个案例返回值

Javascript开关语句多个案例返回值,javascript,json,if-statement,switch-statement,conditional-statements,Javascript,Json,If Statement,Switch Statement,Conditional Statements,我有一个json对象,我从中获取数据,其中包含值(Cow:'Yes',Chicken:'NULL')和(Cow:'Yes',Chicken:'Yes')等。我只想显示包含'Yes'的值,然后显示它们的值。目前,我只能返回包含“是”的第一个案例,例如,它只返回“Cow”,但是如果两者都有“Yes”值,则目标是返回“Cow,Chicken”。我做错了什么?或者如果有更好的方法,谢谢你的帮助 switch ("Yes") { case popup.properties.Beef: retu

我有一个json对象,我从中获取数据,其中包含值(Cow:'Yes',Chicken:'NULL')和(Cow:'Yes',Chicken:'Yes')等。我只想显示包含'Yes'的值,然后显示它们的值。目前,我只能返回包含“是”的第一个案例,例如,它只返回“Cow”,但是如果两者都有“Yes”值,则目标是返回“Cow,Chicken”。我做错了什么?或者如果有更好的方法,谢谢你的帮助

switch ("Yes") {
    case popup.properties.Beef:   return "<span>Cows</span>";
    case popup.properties.Pork: return "<span>Pigs</span>";
    case popup.properties.Sheep:  return "<span>Sheep</span>";
    case popup.properties.Goat: return "<span>Goats</span>";
    case popup.properties.Lamb:  return "<span>Lambs</span>";
    case popup.properties.Rabbit: return "<span>Rabbit</span>";
    case popup.properties.OtherMeat:  return "<span>Other</span>";
    case popup.properties.Chicken: return "<span>Chicken</span>";
    case popup.properties.Turkey:  return "<span>Turkey</span>";
    case popup.properties.Duck: return "<span>Duck</span>";
    case popup.properties.Goose:  return "<span>Geese</span>";
    case popup.properties.Pheasant: return "<span>Pheasants</span>";
    case popup.properties.Quail:  return "<span>Quail</span>";
    case popup.properties.OtherPoultry:  return "<span>Other Poultry</span>";
    default:      return "";
}
开关(“是”){
case popup.properties.Beef:返回“Cows”;
case popup.properties.Pork:返回“Pigs”;
case popup.properties.Sheep:返回“Sheep”;
case popup.properties.Goat:返回“山羊”;
case popup.properties.Lamb:返回“Lambs”;
case popup.properties.Rabbit:返回“Rabbit”;
case popup.properties.OtherMeat:返回“其他”;
case popup.properties.Chicken:返回“Chicken”;
case popup.properties.Turkey:返回“Turkey”;
case popup.properties.Duck:返回“Duck”;
case popup.properties.Goose:返回“Geese”;
case popup.properties.phiasant:返回“phiasants”;
case popup.properties.Quail:返回“Quail”;
case popup.properties.otherfollowing:返回“Other following”;
默认值:返回“”;
}

您可以获取具有所需值的对象,并获取字符串数组

var values = { Beef: "Cows", Pork: "Pigs", Sheep: "Sheep", Goat: "Goats", Lamb: "Lambs", Rabbit: "Rabbit", OtherMeat: "Other", Chicken: "Chicken", Turkey: "Turkey", Duck: "Duck", Goose: "Geese", Pheasant: "Pheasants", Quail: "Quail", OtherPoultry: "Other Poultry" };
    result = Object
        .keys(popup.properties)
        .filter(k => popup.properties[k] === 'Yes')
        .map(k => values[k]);

您可以获取具有所需值的对象并获得字符串数组

var values = { Beef: "Cows", Pork: "Pigs", Sheep: "Sheep", Goat: "Goats", Lamb: "Lambs", Rabbit: "Rabbit", OtherMeat: "Other", Chicken: "Chicken", Turkey: "Turkey", Duck: "Duck", Goose: "Geese", Pheasant: "Pheasants", Quail: "Quail", OtherPoultry: "Other Poultry" };
    result = Object
        .keys(popup.properties)
        .filter(k => popup.properties[k] === 'Yes')
        .map(k => values[k]);

最好使用数组。filter()并过滤掉具有“是”函数的动物只能返回一次。最好使用数组。filter()并过滤掉具有“是”函数的动物只能返回一次。太快了。我只是做了一个类似的回答+太快了。我只是做了一个类似的回答+1.