To convert the "arr" array into a string in ES6, you can use the JSON.stringify() method. Here's an example of how you can achieve it:\n\njavascript\nconst arr = [\n {\n \"label\": \"SOP+1\",\n \"name\": \"sop1\",\n \"sop1\": \"1\"\n },\n {\n \"label\": \"SOP+2\",\n \"name\": \"sop2\",\n \"sop2\": \"2\"\n },\n {\n \"label\": \"SOP+3\",\n \"name\": \"sop3\",\n \"sop3\": \"3\"\n },\n {\n \"label\": \"SOP+4\",\n \"name\": \"sop4\",\n \"sop4\": \"45\"\n },\n {\n \"label\": \"SOP+5\",\n \"name\": \"sop5\",\n \"sop5\": \"5\"\n }\n];\n\nconst sop = \"666\";\n\nconst sopNum = JSON.stringify({ sop, ...arr.reduce((acc, item) => ({ ...acc, [item.name]: item[item.name] }), {}) });\n\nconsole.log(sopNum);\n\n\nThe result will be:\n\nplaintext\n\"{\"sop\":\"666\",\"sop1\":\"1\",\"sop2\":\"2\",\"sop3\":\"3\",\"sop4\":\"45\",\"sop5\":\"5\"}"\n

ES6 将数组转换为字符串:使用 JSON.stringify() 方法

原文地址: https://www.cveoy.top/t/topic/qDtV 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录