最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to get a keyvalue pair for an object - Stack Overflow

programmeradmin7浏览0评论

This question may sound very stupid, but I want to get a key/value pair for my object:

var a = {"e":1,"d":3,"f":4}

I tried with Object.keys(a) // returns ["e", "d", "f"]

this does not get me the values associated with the keys, how can I get both to be able to display in a page.

html:

<div>
  <img src={value}/> //here i want value 1 to be displayed
{key} //with "e"
</div>

how do I do it? Thanks!!

This question may sound very stupid, but I want to get a key/value pair for my object:

var a = {"e":1,"d":3,"f":4}

I tried with Object.keys(a) // returns ["e", "d", "f"]

this does not get me the values associated with the keys, how can I get both to be able to display in a page.

html:

<div>
  <img src={value}/> //here i want value 1 to be displayed
{key} //with "e"
</div>

how do I do it? Thanks!!

Share Improve this question edited Mar 28, 2018 at 1:50 user1234 asked Mar 27, 2018 at 23:52 user1234user1234 3,1596 gold badges57 silver badges117 bronze badges 5
  • 1 Object.values for just the values or maybe use Object.entries to get the key/value pairs. – CRice Commented Mar 27, 2018 at 23:55
  • 1 Object.entries(a).forEach(([key, value]) => do stuff) – Ele Commented Mar 27, 2018 at 23:55
  • Thanks all for your help and answers, it helped me a lot!!!! – user1234 Commented Mar 28, 2018 at 0:52
  • if you know the key, just use the key to access the value... object[key] gives the value – me_ Commented Mar 28, 2018 at 2:52
  • Possible duplicate of best way to get the key of a key/value javascript object – me_ Commented Mar 28, 2018 at 3:03
Add a ment  | 

4 Answers 4

Reset to default 4

Here you go. To get key/value pair from an object, do it like below using pure JavaScript way Object.entries():

var a = {"e":1,"d":3,"f":4}

for (const [key, value] of Object.entries(a)) {
  console.log(`${key} ${value}`);
}

If you want to get a single value:

Just use: *a.e* or *a[e]* return 1 

eg. <img src={a.e} />

If you want to iterate an Object to get value there several ways:

var a = {"e":1,"d":3,"f":4}

Method 1: Using for:

for ( var x in a) {
    console.log('v:',a[x]);
}

Method 2: Using map:

Object.keys(a).map( (k) => {console.log('k',a[k])} );

Method 3: Using foreach:

Object.keys(a).forEach(function(key,index) {
        console.log('a[key]',a[key]);
      });

One way you can do it is like this:

var a = {"e":1,"d":3,"f":4}

 for(var key in a){
  alert(key + ' : ' + a[key])
 }

If you need to iterate over each pair that is. However for in loops are kind of janky in javascript and you should read a bit about going that route if you decide to.

https://jsfiddle/edh4131/hd9p7cxq/

var a = {"e":1,"d":3,"f":4}
let container = document.querySelector('.container')

Object.entries(a).forEach(entry =>{
	let div = document.createElement('div')
  let img = document.createElement('img')
  img.src = entry[1]
  div.textContent = entry[0]
  div.appendChild(img)
  
  container.appendChild(div)
})
<div class="container">
  
</div>

发布评论

评论列表(0)

  1. 暂无评论