我尝试合并两个数组,但是它变得像这张图片一样。
合并后我希望它变得像这样
{0: {…}, storedArr: Array(2)}
0:
address: "ifanio de los Santos Ave, Mandaluyong, 1550 Metro Manila, Philippines"
addressNick: "test1"
latitude: 14.5724682
longitude: 121.0470239
remarks: "No.12"
1:
address: "ifanio de los Santos Ave, Mandaluyong, 1550 Metro Manila, Philippines"
latitude: 14.5724682
longitude: 121.0470239
remarks: "No.12"
__proto__: Object
length: 2
__proto__: Array(2)
__proto__: Obj
这是我的示例代码作为参考
this.storage.get('storedAdd').then((addrr)=>{
let storedArr = [];
console.log(val)
storedArr.push({
address: val.address[0],
addressNick: val.addressNickName,
remarks: val.remarks,
latitude: val.latitude,
longitude: val.longitude
});
console.log(storedArr)
console.log(addrr)
this.storage.set('storedAdd', {...storedArr,...addrr});
console.log({storedArr,...addrr})
const datatransf = {...storedArr,...addrr}
console.log(datatransf)
this.packageAdd$.next(datatransf);
})
答案 0 :(得分:3)
您正在尝试在此处将数组扩展为一个对象。这将在0
索引下的对象内部提供一个数组。
this.storage.set('storedAdd', {...storedArr,...addrr});
您确定这就是您想要的吗?也许应该像这样:
this.storage.set('storedAdd', [...storedArr,...addrr]);
答案 1 :(得分:2)
您正在将数组散布到对象{ ...arr1, ...arr2 }
中,而不是数组[ ...arr1, ...arr2 ]
var arr1 = ['a', 'b', 'c']
var arr2 = ['d', 'e']
var merged = [...arr1, ...arr2]
console.log(merged)