【问题标题】:How to check if an object value is equal to a string in an array?如何检查对象值是否等于数组中的字符串?
【发布时间】:2023-03-11 05:55:01
【问题描述】:

我正在制作一个音乐写作网站,我想知道如何检查数组中的对象和字符串是否相等。我想要的结果是 qnoteC 附加到 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)
}

【问题讨论】:

    标签: javascript html arrays append


    【解决方案1】:

    我不完全理解您的问题,但我相信您可以检查array[0] == string 是否正常。如果您需要根据对象检查对象或根据数组检查数组,只需遍历该对象/数组并检查当前元素是否等于 other 对象/数组中的当前元素。

    【讨论】:

      猜你喜欢
      • 2018-02-10
      • 2022-01-04
      • 2011-12-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-25
      • 1970-01-01
      • 2019-07-06
      相关资源
      最近更新 更多