簡體   English   中英

如何使用 input.value 並從 object 獲取密鑰

[英]How use input.value and get key from object

嗨,有人可以幫我嗎? 我想在輸入中寫“company1”,將它與 input.value 一起使用並顯示在 score(paragraph) 中。

它應該看起來像“寫 company1 並顯示在分數箔 1、箔 2 和箔 3 中”

const input = document.querySelector('input');
const score = document.querySelector('.score');

const prices = {
    'company1': {
        'foil1': {
            '0-5000m2' : 0.67,
            '5000-10000m2' : 0.65,
            '10000-20000m2' : 0.63
        },
        'foil2': {
            '0-5000m2' : 0.67,
            '5000-10000m2' : 0.65,
            '10000-20000m2' : 0.63
        },
        'foil': {
            '0-5000m2' : 0.67,
            '5000-10000m2' : 0.65,
            '10000-20000m2' : 0.63
        }
    }
}

btnSearch.addEventListener('click',()=>{

const company = input.value;
const foil = Object.keys(company) - I know that here is problem, but dont know how make it correctly
console.log(foil); and here is problem becasue its return numbers = Array(8) [ "0", "1", "2", "3", "4", "5", "6", "7" ]
})

你可以喜歡下面這個。 公司名稱是prices的關鍵。 所以你應該將變量作為鍵傳遞。 並使用Object.values然后只有你得到 object 值。這意味着 object 的右側

Object.values(prices[company])

 const input = document.querySelector('input'); const btnSearch = document.querySelector('button'); const prices = { 'company1': { 'foil1': { '0-5000m2': 0.67, '5000-10000m2': 0.65, '10000-20000m2': 0.63 }, 'foil2': { '0-5000m2': 0.67, '5000-10000m2': 0.65, '10000-20000m2': 0.63 }, 'foil': { '0-5000m2': 0.67, '5000-10000m2': 0.65, '10000-20000m2': 0.63 } } } btnSearch.addEventListener('click', () => { const company = input.value; const foil = Object.values(prices[company]) console.log(foil) })
 <input value="company1"/> <button type="button">click</button>

你應該做Object.keys(prices) ,對吧?

你能澄清一下你真正想要做什么嗎? 那我可以更好地幫助你。

暫無
暫無

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

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