0

I'm trying to find a way to reformat an array of JSON objects that looks like:

[{"amount":3,"name":"Coca-Cola"},{"amount":3,"name":"Rib Eye"}] 

I want to only print the values in plain html text. For example:

3 - Coca-Cola
3 - Rib Eye

How can i achieve this? My back end is Node.js so yeah maybe I can do that before building the html i will use.

1 Answer 1

1

Just iterate over your array, saving the formatted strings to another array. You can pass the created array to your page and display it, or do whatever you want.

var json = "[{\"amount\":3,\"name\":\"Coca-Cola\"},{\"amount\":3,\"name\":\"Rib Eye\"}]",
    arr = JSON.parse(json),
    formatted = [];
arr.forEach(function(item) {
  formatted.push(item.amount + " - " + item.name);
});

In the above code, the array formatted contains the strings as you need them.

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

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.