有时候,出于某种目的,需要将数组转化成对象,一个简单快速的方法是就使用展开运算符号(...):
var fruits = [“banana”, “apple”, “orange”, “watermelon”]; var fruitsObj = { …fruits }; console.log(fruitsObj); // returns {0: “banana”, 1: “apple”, 2: “orange”, 3: “watermelon”, 4: “apple”, 5: “orange”, 6: “grape”, 7: “apple”}
合并数组
var fruits = [“banana”, “apple”, “orange”, “watermelon”]; let sddname = [...fruits, 'wewe']
// [
“banana”, “apple”, “orange”, “watermelon”,
'wewe'
]