简体   繁体   中英

How to Convert String to array in js

Actually, I'm making a day wise items like (item name,description,price) like this but now coming same line all the data like (Hot & Crisp - ItemChicken Snacker,description,$8-$5) i want like below that format.

name:"Hot & Crisp
description:
price:"$6"

name":"Chicken Snacker"
description":"A delicious chunk of chicken served in a soft sesame bun, with salad and Thousand Island sauce."
price:"$5"

BUT now my output is like this: (name:Hot & Crisp - ItemChicken Snacker,description:jjjdjdjdjdj,price:$8-$5) Mycode:

var ItemsDetails = v.itemsArray;
var ItemsName = JSON.stringify(ItemsDetails);
                                lg(ItemsName);
                                var myObject = eval('(' + ItemsName + ')');
                                for (i in myObject)
                                {
                                    var itemname = myObject[i]["name"];
                                    html += '<b>'+ itemname +'</b>';
                                }

You can edit this line:

html += '<b>'+ itemname +'</b>';

to:

html += '<b>'+ itemname +'</b><br>';

not really sure, but this may be similar to what you are looking for.

 var ItemsDetails = [{ name: "Hot & Crisp", description: "", price: "$6" }, { name: "Chicken Snacker", description: "A delicious chunk of chicken served in a soft sesame bun, with salad and Thousand Island sauce.", price: "$5" }]; //var ItemsName = JSON.stringify(ItemsDetails); //var myObject = eval('(' + ItemsName + ')'); //for (i in myObject) //{ // var itemname = myObject[i]["name"]; // html += '<b>'+ itemname +'</b>'; //} var htmlOut = ""; for (var i = 0; i < ItemsDetails.length; i++) { htmlOut += '<p>'; htmlOut += 'Name: ' + ItemsDetails[i].name + '<br>'; htmlOut += 'Description: ' + ItemsDetails[i].description + '<br>'; htmlOut += 'Price: ' + ItemsDetails[i].price; htmlOut += '</p>'; } document.getElementById("sampleDiv").innerHTML = htmlOut;
 <div id="sampleDiv"></div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM