我在选择元素(多选)选项中使用了以下字符串数组。
let array = ["one", "two", "three"];
我也有以下对象。
{ "one" : ['apple','orange','banana'], "two" : ['cucumber','onion'], "three" : ['potato','radish','carot']};
我选择一个和两个我想结合['apple','orange','banana']
,并['cucumber','onion']
在另一个选择下拉。
使用Array#map
通过数组进行迭代,并得到相应的阵列中的对象和Array#flat
扁平化的结果:
let array = ["one", "two"];
let obj = {
"one": ['apple', 'orange', 'banana'],
"two": ['cucumber', 'onion'],
"three": ['potato', 'radish', 'carot']
};
const result = array.map(e => obj[e]).flat();
console.log(result);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句