[英]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>
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
循环迭代arr
, Object.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.