0

I have this dynamic JSON -

    {
    "SMSPhone": [
        "SMS Phone Number is not valid"
    ],
    "VoicePhone": [
        "Voice Phone Number is not valid"
    ]
}

I need only values as comma separated string. Also, its dynamic so, i do not know the key names.

Desired result - SMS Phone Number is not valid,Voice Phone Number is not valid

Code i tried -

let res=JSON.parse(Jsondata);
   res.forEach(element => {
    console.log(element)
    //logic
  });

I am getting following error enter image description here

2 Answers 2

1

I don't know what you want to achieve here. But you can't use forEach on an object. forEach is used to loop through data in an array. In your case, I would use object.entries() as shown below to achieve my goal.

const data = {
    "SMSPhone": [
        "SMS Phone Number is not valid"
    ],
    "VoicePhone": [
        "Voice Phone Number is not valid"
    ]
};


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

Sign up to request clarification or add additional context in comments.

Comments

0

res is an object, so you can't use forEach. If you want to iterate over an object's values, you need to do something like:

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

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries

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.