Get key and value of a JavaScript array into variable

前端 未结 6 2038
时光说笑
时光说笑 2021-01-12 16:35

I have a JavaScript object array. When write console.log(myarry) it will show in the console in the below form.

Array[2]
0: Object
one: \"one\"         


        
6条回答
  •  猫巷女王i
    2021-01-12 17:24

    Here's an interesting way to do it.

    const arr = [{ one: 'one' }, { two: 'two' }];
    
    Object.entries(arr).forEach(([_, obj]) => {
      const key = Object.keys(obj)[0];
      console.log(`Key is ${key}, value is ${obj[key]}.`);
    });

提交回复
热议问题