Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How can I get key values and correspond it to each other in js?

I have an array of objects as follow:

[
    {
        "card_key": "Edition",
        "card_value": "Elite Fight Night"
    },
    {
        "card_key": "Card ID",
        "card_value": "15824885587"
    }
]

and I want the output as follow:

{
  "Edition":"Elite Fight Night",
  "Card ID":"15824885587"
}

How can I do this in javaScript? I m beginner of js. Not know how can I do this

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Just iterate through the array and save the value of card_key as key and card_value as the value for your new object.

Something as shown below

    
obj_array = [
    
    {
        "card_key": "Edition",
        "card_value": "Elite Fight Night"
    },
    {
        "card_key": "Card ID",
        "card_value": "15824885587"
    }
    ]

objs1 = {}

for (const x of obj_array)

    objs1[x["card_key"]] = x["card_value"]

console.log(objs1)
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading