简体   繁体   中英

How use input.value and get key from object

Hi can someone help me with this? I want write "company1" in input, take it with input.value and display it in score(paragraph).

Its should look like "write company1 and display in score foil1, foil2 and foil3"

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" ]
})

You could like this below. Company name is a key of prices . so you should pass the variable as key. And use Object.values then only you got the object value.That means right side of 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>

You should do Object.keys(prices) , right?

Could you please clarify what you actually want to do? Then I can help you better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM