从两个数组中找到相似的字符串(obj.key),arr [{},{}] arr2 [{},{}],然后将checked属性设置为true

时间:2017-04-21 19:22:39

标签: javascript angularjs material

感谢您的时间。有两个数组,arr是html页面中的下拉列表,可以是多个选择。提交时,控制器对从arr 商品= councils_item.replace(/(.*)_/,a =>'')获得的选定值进行操作,最后生成一个新格式的格式是arr2。这导致arr中的一些单词由arr2中的商品中的单词组成。例如arr2中的 ITPSM 类似于arr中的 TechProd_ITPSM 。我怎样才能找到arr中与元素arr2 / items类似的元素或者与 Travel相同的元素。并将相应的项选中属性值设置为true?我正在使用angularjs和棱角分明的材料。提前一百万谢谢。顺便说一句下面也是html的一部分。

var arr = [
    { label: 'Techod_ITSM', checked: false },
    { label: 'Techod_Teco', checked: false },
    { label: 'Techod_Connectivity', checked: false },
    { label: 'Services_CF', checked: false },
    { label: 'Technical Services', checked: false },
    { label: 'Travel', checked: false },
    { label: 'Marketing & solutions', checked: false },
    { label: 'All', checked: false },
    { label: 'facility Solution Services', checked: false }
];
var arr2 = [
    { "Marketing & Communications": true },
    { "All": true },
    { "Travel": true },
    { "Technical Products": true, "commodities": ["ITSM", "Teco", "Connectivity"] },
    { "Services": true, "commodities": ["CF", "Technical Services"] }
];

<form name="userForm" novalidate ng-submit="userForm.$valid &&  userProject.createProject()"> 
<md-select ng-model="userProject.project_councils" multiple="true">
 <md-option ng-value="project_councils" ng-repeat="project_councils in project_councilss" ng-selected="project_councils.checked">{{project_councils.label}}
</md-option></md-select>
<md-button type="submit" id="createProject" value="Submit" class="md-raised md-primary" aria-label="Create" > Submit
</md-button>

enter image description here

2 个答案:

答案 0 :(得分:0)

change事件<select>,您可以使用for..of循环来迭代arr2数组,使用Object.entries()迭代每个对象属性,值,检查属性值包含<option>RegExp /[A-Z][a-z]+/g的所选文本,以匹配大写字母,后跟一个或多个小写字母;如果属性为"commodities",则将对象的数组值作为带"label"的字符串连接到当前.join("");如果arr属性为for..of,则使用Object.entries()循环checked迭代arr,将当前索引的true "label"设置为arr {1}}使用相同的select.value RegExp匹配。

&#13;
&#13;
var arr = [
    { label: 'TechProd_ITPSM', checked: false },
    { label: 'TechProd_Telco', checked: false },
    { label: 'TechProd_Connectivity', checked: false },
    { label: 'Services_CWF', checked: false },
    { label: 'Technical Services', checked: false },
    { label: 'TechProd_Telco', checked: false },
    { label: 'Travel', checked: false },
    { label: 'Marketing & solutions', checked: false },
    { label: 'All', checked: false },
    { label: 'Facility Solution Services', checked: false }
];
var arr2 = [
    { "Marketing & Communications": true },
    { "All": true },
    { "Travel": true },
    { "Technical Products": true, "commodities": ["ITPSM", "Telco", "Connectivity"] },
    { "Services": true, "commodities": ["CWF", "Business Services", "Technical Services"] }
];

const select = document.querySelector("select");
const re = /[A-Z][a-z]+/g;

select.onchange = () => {
  const val = select.value.match(re);
  filter: for (let prop of arr2) {
    for (let [key, value, c] of Object.entries(prop)) {
      if (new RegExp(val.join("|")).test(key)) {
        if (/commodities/.test(key)) c = value.join("");
        for (let [index, {label}] of Object.entries(arr)) {
          arr[index]["checked"] = new RegExp(label.concat(c || "").match(re)
                                  .join("|")).test(select.value);
        }              
      }
    }
  }
  console.log(arr);
}

for (let {label, checked} of arr) {
  let option = new Option(label, label);
  option.selected = checked;
  select.appendChild(option);
}
&#13;
<select><option value="">Select an option</option></select>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

固定。使用forEach获取所有商品,然后使用map和reduce来处理&#34; join&#34;字符串&#34; TechProd_ITPSM&#34;将选中的值设置为true。 会有更好的方法。欢迎你的任何想法。

var selectedCom = "";
            var selectedComArr = [];
            feedbackProject.project_councils.forEach(function(resCouncils) {
                var resCommodities = resCouncils.commodities;
                if (resCommodities) {
                    resCommodities.forEach(function(resComItem) {
                        if ((resComItem == "ITPSM") || (resComItem == "Telco") || (resComItem == "Connectivity")) {
                            selectedCom = "TechProd_" + resComItem;
                            selectedComArr.push(selectedCom);
                        } else if ((resComItem == "CWF") || (resComItem == "Business Services") || (resComItem == "Technical Services")) {
                            selectedCom = "Services" + resComItem;
                            selectedComArr.push(selectedCom);
                        }
                    });
                } else {
                    selectedComArr.push(Object.keys(resCouncils)[0]);
                }
                console.log(selectedComArr);
            });

            function reducer(map, that) {
                map[that.label] = that;
                return map;
            }

            function getMapper(map) {
                return function(item) {
                    var temp = map[item];
                    if (temp && temp.label === item) {
                        temp.checked = true;
                    }
                }
            }

            function checkSelected(arr, arr2) {
                var map = arr.reduce(reducer, {});
                arr2.map(getMapper(map));

            };