Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/379.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 基于所选值显示数组中的值(从数组生成字符串)_Javascript_Arrays_Angularjs - Fatal编程技术网

Javascript 基于所选值显示数组中的值(从数组生成字符串)

Javascript 基于所选值显示数组中的值(从数组生成字符串),javascript,arrays,angularjs,Javascript,Arrays,Angularjs,我需要根据从对象数组中选择的值显示自定义字符串值。 我想我已经接近了,但是有一个小问题我想不出来 现在,如果我从“获得”和“静态”中选择所有项 返回语句将是“gound”,如果我从“Static”中选择all,从“gound”中选择一项,结果将是Static HTML块 <a class="customSegmentSelectorButton" title="{{ ::$parent.Labels.SEGMENTSELECT }}"> <span ng-if="tot

我需要根据从对象数组中选择的值显示自定义字符串值。 我想我已经接近了,但是有一个小问题我想不出来

现在,如果我从“获得”和“静态”中选择所有项 返回语句将是“gound”,如果我从“Static”中选择all,从“gound”中选择一项,结果将是Static

HTML块

<a class="customSegmentSelectorButton" title="{{ ::$parent.Labels.SEGMENTSELECT }}">
    <span ng-if="totalSelectedOptions() == 0">No KPIs</span>
    <span ng-if="totalSelectedOptions() > 0">Sum of {{selectedOptionsLabel()}} </span>
</a>

没有KPI
{{selectedOptionsLabel()}之和
组件

customSegmentOptions = [
    {
        Label: "Gained",
        Children: [
            {
                Label: "New",
                Value: "New",
                Selected: true
            },
            {
                Label: "Win",
                Value: "Win",
                Selected: true
            },
            {
                Label: "Add-On",
                Value: "AddOn",
                Selected: true
            }
        ]
    },
    {
        Label: "Static",
        Children: [
            {
                Label: "Restart",
                Value: "Restart",
                Selected: true
            },
            {
                Label: "Repeat",
                Value: "Repeat",
                Selected: true
            }
        ]
    }
];

selectedOptionsLabel = function(){
    var customSegmentOptions = scope.$parent.customSegmentOptions;
    var isGainedSelected = false;
    var isStaticSelected = false;
    var isChildrenSelected = false;
    var selectedChildrenToDisplay = [];

    for(var index = 0; index < customSegmentOptions.length; index++){
        var item = customSegmentOptions[index];
        var children = item.Children.filter(x => x.Selected == true);
        if(item.Children.length == children.length){
            if(item.Label.toLowerCase() == 'gained'){
                isGainedSelected = true;
            }else{
                isStaticSelected = true;        
            }  
        }    
        selectedChildrenToDisplay = selectedChildrenToDisplay.concat(children.map(x => x.Label));                                      
    }

    isChildrenSelected = selectedChildrenToDisplay.length < 5 && !isGainedSelected ? true : false;

    switch (true) {
        case isGainedSelected && !isStaticSelected && !isChildrenSelected:
          return "Gained";
        case isStaticSelected && !isGainedSelected && !isChildrenSelected:
          return "Static";
        case isStaticSelected && isGainedSelected:
           return "Gained & Static";
        case (!isStaticSelected || !isGainedSelected) && isChildrenSelected:
           console.log(selectedChildrenToDisplay);
         return "children"
      }
};
customSegmentOptions=[
{
标签:“获得”,
儿童:[
{
标签:“新”,
值:“新”,
所选:真
},
{
标签:“赢”,
价值:“赢”,
所选:真
},
{
标签:“加载项”,
值:“加载项”,
所选:真
}
]
},
{
标签:“静态”,
儿童:[
{
标签:“重新启动”,
值:“重新启动”,
所选:真
},
{
标签:“重复”,
值:“重复”,
所选:真
}
]
}
];
SelectedOptions标签=函数(){
var customSegmentOptions=scope.$parent.customSegmentOptions;
var isGainedSelected=false;
var isStaticSelected=假;
var isChildrenSelected=false;
var selectedChildrenToDisplay=[];
对于(var index=0;indexx.Selected==true);
if(item.Children.length==Children.length){
if(item.Label.toLowerCase()=='govered'){
isGainedSelected=true;
}否则{
isStaticSelected=true;
}  
}    
selectedChildrenToDisplay=selectedChildrenToDisplay.concat(children.map(x=>x.Label));
}
isChildrenSelected=selectedChildrenToDisplay.length<5&&!isGainedSelected?真:假;
开关(真){
案例被选中&&!统计被选中&!儿童被选中:
返回“已获得”;
案例isStaticSelected&!isGainedSelected&!isChildrenSelected:
返回“静态”;
案例isStaticSelected&&isGainedSelected:
返回“已获得且静态”;
案例(!isStaticSelected | | |!isGainedSelected)&&isChildrenSelected:
console.log(selectedChildrenToDisplay);
返回“儿童”
}
};
我试图构建的是,当所有一个部分都被选中时,我需要返回一个字符串,例如

  • “获得的总和”-如果仅选择了获得
  • “静态总和-如果仅选择静态
  • “获得和静态之和”-如果同时选择了获得和静态
如果未选择一个部分的所有部分,则在适用时,我希望列出用逗号分隔的各个子项名称,最后一个逗号应为“&”,例如-
“New,Win&Restart之和”)

看起来只有在所有子项都选择了“等于真”时,以下内容才计算为真

if(item.Children.length == children.length){
如果我没有弄错获得您想要的输出,您可能会想要:

if(children.length > 0){

因为子项表示列表中的选定项。

请将html作为well@NagaSaiA完成后,我添加了Html代码。谢谢。是的,工作正常,需要修改一下逻辑,这帮了我的忙。谢谢。