Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
305 views
in Technique[技术] by (71.8m points)

javascript - 2个数组之间的匹配值必须将选项HTML设置为禁用(Matched values between 2 arrays must set the option HTML, to disabled)

I'm new to javascript.

(我是javascript新手。)

I have 2 arrays, how can I match them and return an HTML.

(我有2个数组,如何匹配它们并返回HTML。)

If there are values matched in two arrays, the select option is disabled.

(如果两个数组中有匹配的值,则禁用select选项。)

for example:

(例如:)

var array1 = ["8:00AM", "9:00AM", "10:00AM","11:00AM",]
var array2 = ["11:00AM", "11:00AM", "3:00PM"]

array1.map((el, i) => <option key={i} disabled ={
el.includes(array2) ? true: false
}>{el}</option>))

what I would like to happen is if there are values matched between the 2 arrays, in this case "11:00AM", it would return:

(我想发生的是,如果两个数组之间有匹配的值,在这种情况下为“ 11:00 AM”,它将返回:)

<option> 8:00AM</option>
<option> 9:00AM</option>
<option> 10:00AM</option>
<option disabled = {true} > 11:00AM</option>
<option> 12:00PM</option>

something like this.

(这样的事情。)

Is this possible with my current method?

(我目前的方法有可能吗?)

  ask by Heisenberg translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Array.prototype.includes() 's syntax would be

(Array.prototype.includes()的语法为)

arr.includes(valueToFind[, fromIndex])

Also, you can use Template strings as follows:

(另外,您可以如下使用模板字符串 :)

 var array1 = ["8:00AM", "9:00AM", "10:00AM", "11:00AM", ] var array2 = ["11:00AM", "11:00AM", "3:00PM"] const element = document.querySelector('select'); element.innerHTML = array1.map((el, i) => `<option key=${i} ${array2.includes(el) ? 'disabled': ''}>${el}</option>` ).join(''); 
 <select> </select> 


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...