簡體   English   中英

如何檢查 object 值是否等於數組中的字符串?

[英]How to check if an object value is equal to a string in an array?

我正在制作一個音樂寫作網站,我想知道如何檢查 object 和數組中的字符串是否相等。 我想要的結果是 qnoteC 到 append 到 b1b1。 這是我的代碼。 底部的線是我需要解決的。

const notesarray = ['C','Db','D']
const input = document.getElementById('chordsinput')
const b1b1 = document.querySelector('.b1b1')
const b1b2 = document.querySelector('.b1b2')
const b1b3 = document.querySelector('.b1b3')
const b1b4 = document.querySelector('.b1b4')
const qnoteC = document.createElement('img')
const qnoteDb = document.createElement('img')
const qnoteD = document.createElement('img')

//C
qnoteC.src = 'quarternote.png'
qnoteC.style.width = '32px'
qnoteC.style.position = 'relative'
qnoteC.style.bottom = '8px'
//Db
qnoteDb.src = 'quarternoteflat.png'
qnoteDb.style.width = '32px'
qnoteDb.style.position = 'relative'
qnoteDb.style.bottom = '61px'
//D
qnoteD.src = 'quarternote.png'
qnoteD.style.width = '32px'
qnoteD.style.position = 'relative'
qnoteD.style.bottom = '110px'


generate.addEventListener('click', generateclick)


function generateclick(){
index = notesarray.indexOf(chordsinput.value)
if(index >= 0 )
fifth = notesarray[index + 7]
if(fifth == undefined){
fifth = notesarray[index - 5]}

if(fifth == notesarray[0]){
b1b1.appendChild(qnoteC)
}

我不完全理解你的問題,但我相信你可以檢查array[0] == string是否很好。 如果您需要檢查 object 與 object 或數組與數組,只需遍歷該對象/數組並檢查當前元素是否等於另一個對象/數組中的當前元素。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM