简体   繁体   English

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

[英]find the similar string(obj.key) from two array, arr[{},{}] arr2[{},{}],then set checked attribute to true

Thanks for your time.谢谢你的时间。 there are two arrays, the arr is the dropdownlist in the html page which could be multiple select.有两个数组,arr 是 html 页面中的下拉列表,可以是多选。 when submit, the controller do operations on the selected values got from arr commodity = councils_item.replace(/(.*)_/, a => '') And finally generated a new array which format is arr2.提交时,控制器对从 arr商品 = Councils_item.replace(/(.*)_/, a => '')得到的选定值进行操作,最后生成一个格式为 arr2 的新数组。 That results some words in arr are composed of the words from commodities in arr2.这导致 arr 中的一些词由来自 arr2 中的商品的词组成。 such as ITPSM in arr2 is similar to TechProd_ITPSM in arr.如在ITPSM是ARR2在ARR类似于TechProd_ITPSM。 How could I find the elements in arr which is similar to the element arr2/commodities or the same ones such as Travel.我如何才能在 arr 中找到与元素 arr2/commodities 类似的元素或与Travel. And set the corresponding item checked attribute value to true?并将对应的itemchecked属性值设置为true? I'm using angularjs and angular material.我正在使用 angularjs 和 angular 材料。 Thanks a million in advance.预先感谢一百万。 By the way below is part of html as well.顺便说一下,下面也是 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在此处输入图片说明

At change event of <select> you can use for..of loop to iterate arr2 array, iterate each object property, value using Object.entries() , check if property value contains text of selected <option> with RegExp /[AZ][az]+/g to match uppercase letter followed by one or more lower case letters;<select> change事件中,您可以使用for..of循环来迭代arr2数组,使用Object.entries()迭代每个对象属性、值,使用RegExp /[AZ][az]+/g检查属性值是否包含所选<option>文本/[AZ][az]+/g匹配大写字母后跟一个或多个小写字母; if property is "commodities" , concatenate the array value of the object to current "label" as a string with .join("") ;如果 property 是"commodities" ,则将对象的数组值连接到当前的"label"作为带有.join("")的字符串; iterate arr using for..of loop, Object.entries() , set checked of arr at current index to true if "label" property of arr contains a match to select .value using same RegExp .使用for..of循环迭代arrObject.entries() ,如果arr "label"属性包含匹配以使用相同的RegExp select .value ,则将当前索引处的arr checked设置为true

 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 = /[AZ][az]+/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); }
 <select><option value="">Select an option</option></select>

fixed.固定的。 use forEach to get all commodities and then use map and reduce to deal with a "join" string "TechProd_ITPSM" to set the checked value as true.使用forEach获取所有商品,然后使用map和reduce处理“join”字符串“TechProd_ITPSM”,将checked值设置为true。 There would be better approach.会有更好的办法。 Welcome any of your idea.欢迎您的任何想法。

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));

            };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否有一个loadash函数可以比较两个数组并仅在arr1中存在来自arr2的所有值时才返回true? - Is there a loadash function to compare two arrays and return true only if all values from arr2 is present in arr1? 如何连接arr1数组和arr2数组(基于arr2数组结构) - How to join arr1 array and arr2 array (based on arr2 array structure) 制作一个接收两个 arrays 的 function。 将 arr1 和 arr2 中的所有数字相加。 如果 arr1 的总和等于 arr2 返回 true。 如果不是,则为假 - make a function that takes in two arrays. Add all the numbers in arr1 & arr2. If the sum of arr1 is equal to arr2 return true. False if not 过滤 arr1 &gt; obj1 &gt; arr2 &gt; obj2 中的多个第一个对象 - filter multiple first objects in arr1 > obj1 > arr2 > obj2 arr1.concat(arr2)和[] .concat(arr1,arr2)之间的区别 - Difference Between arr1.concat(arr2) And [].concat(arr1, arr2) 如何在 JavaScript 中将数组 (arr1) 的值推送到新的空数组 (arr2) 中? - How to push a value of an array (arr1) into a new empty array (arr2) in JavaScript? 如何从arr1和arr2获取结果,当ID匹配时我需要从arr1复制内容 - How can I get the result from arr1 and arr2, When the ID matches I need to copy the content from arr1 如何解构obj [arr [0]] = arr [1] - How to destructure obj[arr[0]] = arr[1] 在给定索引 n 处将 arr1 复制到 arr 2,并且在 function 运行后 arr 1 和 arr2 应该相同 - copy arr1 to arr 2 at given index n and arr 1 and arr2 should be same after function runs 我正在尝试编写 function 其中 arr1[0] = arr2[0], arr1[1] = arr2[1], ... 等等。 我该怎么做呢? - I am trying to write a function where arr1[0] = arr2[0], arr1[1] = arr2[1], … and so on. How do I do this?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM