0

Trying to get both key and value using the following script

jsonData = {"jurisdiction":"SCPB - LON","firstName":"David Dynamic"}

var keys   = Object.keys(jsonData).map(function(keys, values) {
    logInfo(jsonData[keys], jsonData[values]);              
});

returns just the values:

2022-08-30 18:29:49 David Dynamic undefined
2022-08-30 18:29:49 SCPB - LON undefined

How can I get both? the js engine is spiderMonkey 1.8

1
  • 1
    Object.entries Commented Aug 30, 2022 at 17:44

2 Answers 2

3
Object.keys(jsonData).forEach(function(key) {
    logInfo(key, jsonData[key]);              
});

Object.keys documentation

Or as suggested by Kondrak Linkowski:

Object.entries(jsonData).forEach(([key, value]) => logInfo(key, value))
Sign up to request clarification or add additional context in comments.

Comments

2

You likely want to use Object.entries to get the keys and values of the object.

If you want to do it with Object.keys, as in your example, you can modify it like this: (logging the key, and the value at this key)

const jsonData = {
 foo: 'bar',
 fizz: 'fuzz',
}
const logInfo = console.log

var keys = Object.keys(jsonData).map(function(key) {
    logInfo(key, jsonData[key]);              
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.